简体   繁体   English

Python 中的随机配方生成器

[英]Random Recipe Generator in Python

I have to create a random recipe generator in Python.我必须在 Python 中创建一个随机配方生成器。 Here are the design specifications:以下是设计规范:

  • At least 25 different ingredients and 10 different “cooking phrases” to draw from.至少有 25 种不同的成分和 10 种不同的“烹饪短语”可供借鉴。

  • Recipes consist of an ingredient list and a description of the process.食谱由成分表和过程描述组成。 The process should mention the same ingredients that are in the list.该过程应提及列表中的相同成分。

  • Randomly generate amounts for the ingredients as well as the ingredient names.随机生成成分的数量以及成分名称。 When in doubt, measure everything in grams.如有疑问,请以克为单位测量所有内容。

  • Organize your program into functions.将您的程序组织成函数。 You should have at least 2 user-defined functions.您应该至少有 2 个用户定义的函数。

I have written some code but I am having trouble with using the same randomly generated ingredients and adding them to the end of the "recipe instructions" sentences.我已经编写了一些代码,但是在使用相同的随机生成的成分并将它们添加到“食谱说明”句子的末尾时遇到了麻烦。

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)

When you generate the ingredients with random.sample() instead of random.choice() , you get a list of unique ingredient without duplicates.当您使用random.sample()而不是random.choice() ) 生成成分时,您会得到一个没有重复的唯一成分列表。 See the example below:请参见下面的示例:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM