<p>这里是Python新手,使用python2.7。我正在创建一个程序,打印出一份随机的食谱,里面有配料和说明。我会在最后公布我的代码。我得到的输出是:</p>
<blockquote>
<p>Here is a recipe('Sushi', ['tuna', 'rice', 'mayonnaise', 'wasabi'])</p>
<ol>
<li>Wash off the tuna</li>
</ol>
</blockquote>
<p>但我想要这个:</p>
<blockquote>
<p>Here is a recipe: Sushi: tuna, rice, mayonnaise, wasabi</p>
<ol>
<li>Wash off the tuna</li>
</ol>
</blockquote>
<p>我可以使用format()方法来完成类似的任务吗?在</p>
<p>这是我的代码:</p>
<pre><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"
</code></pre>