Python新手這裏,使用Python 2.7格式字典鍵和值。我正在創建一個程序,用它的原料和說明打印出一份隨機配方。我會在最後發佈我的代碼。我得到的輸出是:如何使用格式()
這裏是一個配方( '壽司',[ '金槍魚', '飯', '蛋黃醬', '綠芥末'])
- 洗掉金槍魚
但我想這一點:
這裏是一個食譜:壽司:金槍魚,大米,蛋黃醬,芥末
- 洗掉金槍魚
我可以使用格式()方法來完成這樣的事情?
這裏是我的代碼:
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"
一些需要注意的是,這將始終打印' 1。因爲'recipe_dict.keys()== ['Pasta','Sushi','ChocolateCake']'無論食譜如何,都可以洗掉金槍魚,永遠不會是elif中的任何一種。 – timotree
謝謝@timotree – johnnewbie25