代码不适用于 if/else 语句


print("Select operation.")
print("1.Price of your luggage")
print("2.Exit")

# Take input from the user 
choice = input("Enter choice(1/2:")
if choice == '1':
num1=(input("Enter wieght of your first  luggage " ))
if num1 <=15:
print('Your first item is free')
elif num1 >=15:
print('Your items will cost you money')
elif choice == '2':
raise SystemExit()

这是我评估的开始。我可以帮忙吗?

您的代码存在一些缩进问题,并且用户输入始终为str(字符串(类型。您需要在检查 if 语句之前将权重转换为int。您还应该在没有=的情况下使用num1 > 15,因为您已经在num1 <= 15中使用过它。这是经过纠正的代码。正如 khelwood 在评论中指出的那样,您的elif可以简单地替换为else,因为如果数字不小于 15,它将大于 15。

print("Select operation.")
print("1.Price of your luggage")
print("2.Exit")
# Take input from the user 
choice = input("Enter choice(1/2:")
if choice == '1':
num1 = int(input("Enter wieght of your first  luggage " ))
if num1 <= 15:
print('Your first item is free')
else:
print('Your items will cost you money')
elif choice == '2': 
raise SystemExit()

Select operation.
1.Price of your luggage
2.Exit
Enter choice(1/2:1
Enter wieght of your first  luggage 12
Your first item is free