Программа генерирует случайные рецепты в python - PullRequest
0 голосов
/ 03 апреля 2020

Я пишу код, который генерирует случайные рецепты. Это должно быть структурировано так.

*** <main_ingredient> <baking> Recipe ***
Ingredients: 
<quantity> <measure> <main_ingredient>
<quantity> <measure> <ingredient>
<quantity> <measure> <ingredient>
<quantity> <measure> <ingredient>
<quantity> <measure> <ingredient>
<quantity> <measure> <ingredient>
Method:
Mix the <ingredient>, <ingredient>, <ingredient>, <ingredient> and <ingredient> together. 
Gently fold in the <main_ingredient>. 
Place mixture in <baking> tin and bake at <temperature> degrees Celsius for <time> minutes. 
***

и у меня есть этот код на данный момент

import random
main_ingredient = ['chocolate', 'banana', 'salted caramel', 'carrot', 'bran']
baking = ['cake', 'brownie', 'cupcake', 'muffin']
measure = ['tbsp', 'tsp', 'cup']
ingredient = [ 'flour', 'baking powder', 'butter', 'milk', 'eggs', 'vanilla', 'sugar']

m_ingredient = random.choice(main_ingredient)
baking_ = random.choice(baking)
ingredients = [random.choice(ingredient), random.choice(ingredient)]

print ("***", m_ingredient.title(), baking_.title() , "Recipe ***")
print ("Ingredients:")

for i in ingredient:
    print (random.randint(1,3), random.choice(measure), i)

Есть ли другой способ написать этот код? Может ли кто-нибудь дать мне представление о том, что использовать и как это сделать?

1 Ответ

0 голосов
/ 03 апреля 2020

Вы можете напечатать строку с вашим основным ингредиентом перед вашим l oop, и поместить все это в функцию, чтобы она могла печатать несколько рецептов в al oop, как это

import random

main_ingredient = ['chocolate', 'banana', 'salted caramel', 'carrot', 'bran']
baking = ['cake', 'brownie', 'cupcake', 'muffin']
measure = ['tbsp', 'tsp', 'cup']
ingredient = [ 'flour', 'baking powder', 'butter', 'milk', 'eggs', 'vanilla', 'sugar']

def create_recipe(main_ingredient, baking, measure, ingredient):
    "Create a random recipe and print it."""
    m_ingredient = random.choice(main_ingredient)
    baking_ = random.choice(baking)
    ingredients = [random.choice(ingredient), random.choice(ingredient)]

    print("***", m_ingredient.title(), baking_.title() , "Recipe ***")
    print("Ingredients:")
    print(random.randint(1,3), random.choice(measure), m_ingredient)
    for i in ingredient:
        print(random.randint(1,3), random.choice(measure), i)

# create multiple random recipes
for i in range(3):
    create_recipe(main_ingredient, baking, measure, ingredient)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...