如何求解:[ValueError:int()的无效文本,基为10:]


command= " "
while True:
command = int(input("how old are you? "))
if command <= 12:
print("you're ticket is free.")
elif command <= 15:
print("you're ticket is $20.")
elif command > 15:
print("you're ticket is $50.")
elif command == "exit":
break

当我写exit时,它给出了ValueError

ValueError: invalid literal for int() with base 10:'exit'

在尝试将命令转换为int:之前,您应该检查命令是否已退出

while True:
command = input("how old are you? ")
if command == "exit":
break
command = int(command)
if command <= 12:
print("you're ticket is free.")
elif command <= 15:
print("you're ticket is $20.")
elif command > 15:
print("you're ticket is $50.")

最新更新