[英]'except' in try statement (inside while loop) isn't working
I've written a program which repeatedly reads numbers until the user enters "done"
.我编写了一个程序,它反复读取数字,直到用户输入
"done"
。 If the user enters anything other than a number, detect their mistake using try and except and print an error message and skip to the next number.如果用户输入的不是数字,请使用 try 和 except 检测他们的错误并打印错误消息并跳到下一个数字。
The problem is the code never gets to the except
part of the code (user should get an error for not entering anything or for entering invalid characters (anything not 'done'
or a number). What am I doing wrong?问题是代码永远不会到达代码的
except
部分(用户应该因为未输入任何内容或输入无效字符(任何未'done'
或数字)而收到错误消息)。我做错了什么?
numbers=(input("Enter a number or 'done' to quit: "))
count=1
total_num=int(numbers)
while True:
numbers=(input("Enter another number or 'done' to quit: "))
try:
if (numbers.isalpha() and numbers=='done'): #elif numbers.isalpha() and numbers=='done':
break
elif numbers.isdigit():
numbers=int(numbers)
total_num+=int(numbers)
count+=1
except:
print('Valid a number or done to quit:' )
print('sum is', total_num)
print('count is', count)
print('average is', int(total_num/count))
You've misunderstood the point of the try..except
block.您误解了
try..except
块的意义。 You don't need to check if the input isdigit
, since that precludes any errors from being thrown.您无需检查输入是否为
isdigit
,因为这可以防止抛出任何错误。 Just try to convert the input to int
, and if this fails, you'll fall through to the except
.只需尝试将输入转换为
int
,如果失败,您将进入except
。 There, you can check if the input is == "done"
, and break out:在那里,您可以检查输入是否为
== "done"
,然后中断:
while True:
numbers = input("Enter another number or 'done' to quit: ")
try:
numbers = int(numbers)
total_num += numbers
count += 1
except ValueError:
if numbers == "done": break
print('Valid a number or done to quit:' )
Note that I qualified the except
clause to only catch ValueError
s, since it is a bad practice to catch all errors -- that can hide actual problems with your code, and disable the ability to interrupt the program with Ctrl+C
请注意,我将
except
子句限定为仅捕获ValueError
,因为捕获所有错误是一种不好的做法——这可能会隐藏代码的实际问题,并禁用使用Ctrl+C
中断程序的能力
There is nothing that apparently breaks the code so the Except statement is never executed.没有什么明显破坏代码的东西,因此永远不会执行 except 语句。
You can put an else statement with same thing as Except:您可以使用与除外相同的内容添加 else 语句:
numbers=(input("Enter a number or 'done' to quit: "))
count=1
total_num=int(numbers)
while True:
numbers=(input("Enter another number or 'done' to quit: "))
try:
if (numbers.isalpha() and numbers=='done'): #elif numbers.isalpha() and numbers=='done':
break
elif numbers.isdigit():
numbers=int(numbers)
total_num+=int(numbers)
count+=1
else:
print('Valid a number or done to quit:' )
except:
print('Valid a number or done to quit:' )
print('sum is', total_num)
print('count is', count)
print('average is', int(total_num/count))
Unless you find some input that breaks your code I suggest removing it and stay only with the if elif else除非您发现某些输入会破坏您的代码,否则我建议将其删除并仅使用 if elif else
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.