-1

Basically, I have this random meal code that asks the user to input the number 1. Once the user inputs the number 1, I want the if function to work and output the function create_recipe to create a randomly generated cake recipe. However, when the user inputs the number 1, the code does not work. How can I fix this? Is it because of my indentation? Please can someone help me. All responses are much appreciated. Here is the link: https://repl.it/@HarryBradley/ResponsibleForsakenAbilities#main.py Here is the code:

    num = input("Enter the number 1 ")
if num == 1:
  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):
    m_ingredient = random.choice(main_ingredient)
    baking_ = random.choice(baking)
    ingredients = [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_recipe(main_ingredient, baking, measure, ingredient) 
0

2 Answers 2

1

input(...) always retuns a string. You need to cast it to an integer:

num = int(input("Enter the number 1 "))

Or write:

if num == "1":

Better yet: use a try/except block:

try:
    num = int(input("Enter the number 1 "))
except ValueError:
    pass
Sign up to request clarification or add additional context in comments.

Comments

0

you are comparing int with string as in python input() returns String
use num=int(input())

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.