我是Python的新手,它创建了一个程序,可以累加某人的动物数量。
如果用户输入字符串(例如,键入“五”或其他任何字符,程序将中断)。
我该怎么做才能阻止程序中断并使用户返回问题选项?
我创建了一个覆盖整数的循环和else语句。
loop = 1
while loop == 1:
a = int(input(
"Select A Question:"
"Dog Question: '1'"
"Cat Question: '2'"
"Rabbit Question: '3'"
"Calculate Total Animals: '4'"
"Enter one of the above options 1-4:"))
if a == 1:
f = int(input("How many doggos do you have?: "))
elif a == 2:
h = int(input("How many cats do you have?: "))
elif a == 3:
s = int(input("How many rabbits do you have?: "))
elif a == 4:
loop = 0
else:
print("Please enter a valid value e.g. '1', '2', '3'"
"To add up your animals '4'")
答案 0 :(得分:1)
首先,您无需分配loop = 1
; while True
就可以了。
接下来,原因是您试图将input()
的结果转换为int
,无论是否可能。除非这样做,否则应该对它执行一些检查,并且仅在满足以下条件的情况下才对break
进行循环检查,例如:
input_prompt = ("Select A Question:"
"Dog Question: '1'"
"Cat Question: '2'"
"Rabbit Question: '3'"
"Calculate Total Animals: '4'"
"Enter one of the above options 1-4:")
while True:
a = input(input_prompt)
if a.isdigit() and 1 <= int(a) <= 4:
a = int(a)
break
else:
print("Please enter a valid value e.g. '1', '2', '3'"
"To add up your animals '4'")
答案 1 :(得分:0)
如果要防止程序中断,修复很简单。您只需要将输入代码更改为input()而不是int(input())。用int()将输入括起来会强制用户输入为整数。
如果要将其返回给用户输入,请执行以下操作以检查它是否不是整数。
if a == 1:
f = input("How many doggos do you have?: ")
if not f.isdigit():
pass
希望这会有所帮助!
答案 2 :(得分:0)
尝试使用代码:
loop = 1
while loop == 1:
try:
a = int(input(
"Select A Question:"
"Dog Question: '1'"
"Cat Question: '2'"
"Rabbit Question: '3'"
"Calculate Total Animals: '4'"
"Enter one of the above options 1-4:"))
loop = 0
except:
pass
if a == 1:
f = int(input("How many doggos do you have?: "))
elif a == 2:
h = int(input("How many cats do you have?: "))
elif a == 3:
s = int(input("How many rabbits do you have?: "))
elif a == 4:
loop = 0
else:
print("Please enter a valid value e.g. '1', '2', '3'"
"To add up your animals '4'")
答案 3 :(得分:0)
最好,如果您只是刚开始输入字符串,然后尝试获取整数。这样,您的代码就不会很快失败。您可以像这样解决您的示例:
# you need to initialise the variables in order to avoid an error when
# summing them up
number_of_dogs = 0
number_of_cats = 0
number_of_rabbits = 0
while True:
# Ask for the desired mode after each 'secondary entry'
a = input(
"Select A Question:*' \n\
Dog Question: '1' \n\
Cat Question: '2' \n\
Rabbit Question: '3' \n\
Calculate Total Animals: '4' \n\
Enter one of the above options 1-4:")
if a is '1':
# get the user input as a string
dogs = input("How many doggos do you have?: ")
try:
# try to convert it (you can also combine this with the prior
# input(...) but then you're more likely to catch exceptions
# you don't want to catch)
number_of_dogs = int(dogs)
except:
print("please just enter a digit")
elif a is '2':
cats = input("How many cats do you have?: ")
try:
number_of_cats = int(cats)
except:
print("please just enter a digit")
elif a is '3':
rabits = input("How many rabbits do you have?: ")
try:
number_of_rabbits = int(rabits)
except:
print("please just enter a digit")
elif a is '4':
print("You have "
+ str(number_of_dogs
+ number_of_cats
+ number_of_rabbits)
+ " animals")
break
else:
print("Please enter a valid value e.g. '1', '2', '3'"
"To add up your animals '4'")
还有一个小技巧,请尝试使用更多解释性的变量名,它使代码更具可读性。而且,您还可以在第一个输入语句中使用更多的逻辑选择:“狗”,“猫”等,而不是1,2,3 ...