【发布时间】:2021-06-02 16:40:58
【问题描述】:
我必须在 Python 中创建一个随机配方生成器。以下是设计规范:
-
至少 25 种不同的食材和 10 种不同的“烹饪短语”可供借鉴。
-
食谱由成分列表和过程描述组成。该过程应提及列表中的相同成分。
-
随机生成成分的数量以及成分名称。如有疑问,请以克为单位测量所有内容。
-
将您的程序组织成函数。您应该至少有 2 个用户定义的函数。
我已经编写了一些代码,但在使用相同的随机生成的成分并将它们添加到“食谱说明”句子的末尾时遇到了问题。
import random
def ingredients_list(quantity, ing):
quantity = random.choice(amounts)
ing = random.choice(ingredients)
return("{} {}".format(quantity, ing))
def recipe_instructions(recipe):
recipe = random.choice(cooking_phrases)
return("{}".format(recipe))
amounts = ["50g", "70g", "90g", "143g", "150g", "180g", "200g", "235g", "300g", "320g", "350g", "386g", "400g", "433g", "462g"]
ingredients = ["eggs", "sugar", "salt", "butter", "milk", "bacon", "tomatoes", "pasta", "rice", "potatoes", "onions", "carrots", "all-purpose flour", "noodles", "cheese", "chicken", "beef", "spinach", "strawberries", "apples", "blueberries", "olive oil", "soy sauce", "corn", "shrimp", "mayonnaise"]
cooking_phrases = ["In a medium sized bowl, whisk together the", "In a large skillet over medium heat, stir-fry the", "In a skillet, Sauté the", "In a large pot, boil the", "Thoroughly wash the", "Thinly slice the", "Make small, 1-inch cubes by dicing up the", "Set the timer for 1 minute and microwave the", "Using a blender, thoroughly blend the", "For 20 minutes in a 350°F oven, bake the"]
quantity = random.choice(amounts)
ing = random.choice(ingredients)
recipe = random.choice(cooking_phrases)
for i in range(5):
print(ingredients_list(quantity, ing))
print ("Recipe Instructions:")
for i in range(5):
print(recipe_instructions(recipe) + " " + ing)
【问题讨论】:
-
你能比“遇到麻烦”更具体吗?给minimal reproducible example。
-
您的问题在哪里?你有错误要显示吗?有一些不好的代码实践,您在函数中使用了全局常量(例如
random.choice(amounts))。要么将其作为参数传递给函数,要么明确表示它们是全局常量。 -
如果能把现在得到的结果和预期的结果都包含进去会更好。
标签: python