2017-06-17 1 views
1

Python-Neuling hier, mit Python 2.7. Ich erstelle ein Programm, das ein zufälliges Rezept mit seinen Zutaten und Anweisungen ausgibt. Ich werde meinen Code am Ende veröffentlichen. Der Ausgang Ich erhalte ist:Wie formatiere ich Wörterbuch Schlüssel und Werte mit Format()

Hier ein Rezept ist ('Sushi', [ 'Thunfisch', 'Reis', 'Mayonnaise', 'Wasabi'])

  1. abwaschen der
  2. Thunfisch

Aber ich möchte dies:

Hier ist ein Rezept: Sushi: Thunfisch, Reis, Mayonnaise, Wasabi

  1. Wash vom Thunfisch

Kann ich das Format() -Methode, so etwas zu erreichen?

Hier ist mein Code:

import random 

def random_recipe(): 
    recipe_dict = {'ChocolateCake':['flour', 'eggs', 'chocolate', 'oil', 'frosting'], 
        'Pasta':['noodles', 'marinara','onions'], 
        'Sushi':['tuna','rice','mayonnaise','wasabi']} 


    print "Here is a recipe" + str(random.choice(list(recipe_dict.items()))) 

    if recipe_dict.keys() == 'ChocolateCake': 
     print "1. Mix the flour with the eggs" 
    elif recipe_dict.keys() == 'Pasta': 
     print "1. Boil some water" 
    else: 
     print "1. Wash off the tuna" 
+0

Etwas zu beachten ist, dass dies immer drucken ' 1. Spülen Sie den Thunfisch ab, unabhängig vom Rezept, weil 'rezept_dict.keys() == ['Pasta', 'Sushi', 'ChocolateCake']' und niemals eines der Dinge im oder elif sein werden. – timotree

+0

Danke @timotree – johnnewbie25

Antwort

1

Da Sie Tupel von Zufalls abrufen, finden Sie die folgenden Arbeits Code

import random 

recipe_dict = {'ChocolateCake':['flour', 'eggs', 'chocolate', 'oil', 'frosting'], 
       'Pasta':['noodles', 'marinara','onions'], 
       'Sushi':['tuna','rice','mayonnaise','wasabi']} 


ra_item = random.choice(list(recipe_dict.items())) 
print "Here is a recipe {}:{}".format(ra_item[0],','.join(ra_item[1])) 

if recipe_dict.keys() == 'ChocolateCake': 
    print "1. Mix the flour with the eggs" 
elif recipe_dict.keys() == 'Pasta': 
    print "1. Boil some water" 
else: 
    print "1. Wash off the tuna" 

Sie die erwartete Ausgabe mit diesem Code.

+0

Danke. Was macht die Join-Methode in Python? – johnnewbie25

+0

Es ist eigentlich eine String-Funktion. Es nimmt die Werte innerhalb der iterablen (Liste in Ihrem Fall) und Join mit dem Zeichen, das Sie in den Anführungszeichen erwähnen. Geben Sie eine Antwort, wenn Sie die Antwort akzeptieren :) – Arockia

3

Sie können join() verwenden Ihre dict Werte wie in diesem Beispiel anschließen:

from random import choice 

recipe_dict = {'ChocolateCake':['flour', 'eggs', 'chocolate', 'oil', 'frosting'], 
        'Pasta':['noodles', 'marinara','onions'], 
        'Sushi':['tuna','rice','mayonnaise','wasabi']} 

# Or you can unpack your data: 
# key, val = choice(recipe_dict.items()) 
keys = list(recipe_dict.keys()) 
random_key = choice(keys) 
# Using str.format() 
print "Here is a recipe: {}: {}".format(random_key, ', '.join(recipe_dict[random_key])) 

if random_key == 'ChocolateCake': 
    print "1. Mix the flour with the eggs" 
elif random_key == 'Pasta': 
    print "1. Boil some water" 
else: 
    print "1. Wash off the tuna" 
0

Ersatz a mit Ihrem recipe_dict in dem folgenden Code:

for k, v in a.items(): 
    print(k + ': ' + ','.join(map(str, v))) 
Verwandte Themen