Python if语句没有运行

时间:2015-02-26 19:53:34

标签: python python-3.x

当运行时,只需按下一个直到你输入所有需要修复的测量单位,因为我想自己完成剩下的工作。无论如何当你按下它时,它会贯穿所有elif语句。我只需要它以便它不会遍历所有if语句,我不知道它是否与我的布局有关但我非常想要一些帮助谢谢。

file.write(Ingredients)
IngredientA = input("What is the ingredient in this recipie: ")
file.write("Ingredients: \n")
file.write(IngredientA)
file.write("\n")
NumA = input("1. Grams \n 2. Milliliters\n 3.Litres\n 4.Slices\n 5.No specific measurement\n What units of measurement are used for this ingredient (1-5): ")
if NumA == 1:
    file.write("Grams")
    Amount = input("How much of this ingredient in grams does the respie contain: ")
    file.write("\n")
    file.write("Amount of ingredient:\n")
    file.write(Amount)
    file.write("\n")
    Amount = input("How much of this ingredient in Milliliters does the recipie contain: ")
elif NumA == 3:
    file.write("Litres")
    Amount = input("How much of this ingredient in Litres does the recipie contain: ")
    file.write("\n")
    file.write("Amount of ingredient:\n")
    file.write(Amount)
    file.write("\n")
elif NumA == 4:
    Amount = input("How Many slices of this ingredient does the recipie contain: ")
    file.write("\n")
    file.write("Amount of ingredient:\n")
    file.write(Amount)
    file.write("\n")
elif NumA == 5:
    file.write("No specific unit of measurement\n")
    Amount = input("How much of this ingrediant does the recipie contain: ")
    file.write("Amount of ingredient:\n")
    file.write(Amount)
    file.write("\n")

2 个答案:

答案 0 :(得分:2)

你从input回来的东西是一个字符串。字符串"1"永远不会等于整数1。您需要将输入值转换为整数,或将输入与字符串而不是整数进行比较。

答案 1 :(得分:1)

变化

NumA = input("1. Grams \n 2. Milliliters\n 3.Litres\n 4.Slices\n 5.No specific measurement\n What units of measurement are used for this ingredient (1-5): ")

NumA = int(input("1. Grams \n 2. Milliliters\n 3.Litres\n 4.Slices\n 5.No specific measurement\n What units of measurement are used for this ingredient (1-5): "))