python新手代码练习format怎么⽤python,如何使⽤format()格式化字典键和值这⾥是Python新⼿,使⽤python2.7。我正在创建⼀个程序,打印出⼀份随机的⾷谱,⾥⾯有配料和说明。我会在最后公布我的代码。我得到的输出是:Here is a recipe('Sushi', ['tuna', 'rice', 'mayonnaise', 'wasabi'])Wash off the tuna
但我想要这个:Here is a recipe: Sushi: tuna, rice, mayonnaise, wasabiWash off the tuna
我可以使⽤format()⽅法来完成类似的任务吗?在
这是我的代码: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"
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论