Python中的随机配方生成器

Python中的随机配方生成器,python,Python,我必须用Python创建一个随机配方生成器。以下是设计规范: 至少有25种不同的配料和10种不同的“烹饪短语”可供借鉴 配方包括配料表和工艺说明。该过程应提及列表中的相同成分 随机生成成分数量以及成分名称。如有疑问,请以克为单位测量每件物品 将程序组织成函数。您应该至少有2个用户定义的函数 我已经写了一些代码,但我在使用相同的随机生成的成分并将它们添加到“配方说明”语句末尾时遇到了问题 import random def ingredients_list(quantity, ing

我必须用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)

当您使用
random.sample()
而不是
random.choice()
生成配料时,您会得到一个不重复的唯一配料列表。请参见下面的示例:

import random

def ingredients_list(quantity, ing):
    quantity = random.choice(amounts)
    # ing = random.choice(ingredients) This one can generate duplicates
    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"]

steps = 5

quantity = random.choice(amounts)
ings = random.sample(ingredients, k=steps) # use random.sample to generate unique ingredients and prevent duplicates
recipe = random.choice(cooking_phrases)

for i in range(steps):
    ing = ings[i]
    print(ingredients_list(quantity, ing))

print ("Recipe Instructions:")
for i in range(steps):
    ing = ings[i]
    print(recipe_instructions(recipe) + " " + ing)

你能比“有麻烦”更具体一点吗?给我一个答案。你的问题在哪里?你有错误要显示吗?有一些错误的代码实践,您在函数中使用全局常量(例如
random.choice(amounts)
)。要么将其作为参数传递给函数,要么明确它们是全局常量。如果能包含现在得到的结果和预期的结果,效果会更好。@anna li这解决了你的问题吗?