Python函数不输出用户输入的结果

Python函数不输出用户输入的结果,python,Python,基本上,我有一个随机用餐代码,要求用户输入数字1。一旦用户输入数字1,我希望if函数工作,并输出函数create_recipe来创建随机生成的蛋糕配方。但是,当用户输入数字1时,代码不起作用。我怎样才能解决这个问题?是因为我的压痕吗?有人能帮我吗。非常感谢所有回复。以下是链接:以下是代码: num = input("Enter the number 1 ") if num == 1: main_ingredient = ['chocolate', 'banana'

基本上,我有一个随机用餐代码,要求用户输入数字1。一旦用户输入数字1,我希望if函数工作,并输出函数create_recipe来创建随机生成的蛋糕配方。但是,当用户输入数字1时,代码不起作用。我怎样才能解决这个问题?是因为我的压痕吗?有人能帮我吗。非常感谢所有回复。以下是链接:以下是代码:

    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) 
输入(…)
始终重新运行字符串。您需要将其强制转换为整数:

num = int(input("Enter the number 1 "))
或者写:

if num == "1":
更好的方法是:使用try/except块:

try:
    num = int(input("Enter the number 1 "))
except ValueError:
    pass

您将int与string进行比较,就像python中的
input()
返回string
使用
num=int(输入())