通过验证简单输入进行异常处理
Exception Handling by validating simple inputs
我是 Python 的新手,在我设法学习 Java 之后达到了高级水平。
在 Java 中,使用异常处理进行输入验证对我来说从来都不是问题,但在 python 中,我有点困惑:
这是一个简单的 FizzBuzz 程序示例,它只能读取 0 到 99 之间的数字,否则必须抛出异常:
if __name__ == '__main__':
def fizzbuzz(n):
try:
if(0<= n <= 99):
for i in range(n):
if i==0:
print("0")
elif (i%3==0 and i%7==0) :
print("fizzbuzz")
elif i%3==0:
print("fizz")
elif i%7==0:
print("buzz")
else:
print(i)
except Exception:
print("/// ATTENTION:The number you entered was not in between 0 and 99///")
try:
enteredNumber = int(input("Please enter a number in between 0 and 99: "))
fizzbuzz(enteredNumber)
except Exception:
print("/// ATTENTION: Something went wrong here. Next time, try to enter a valid Integer ////")
如果我 运行 并输入例如123 代码刚刚终止,没有任何反应。
如果你想捕捉异常,你需要确保它会在期望的场景发生时被引发。
由于try和except之间的代码块本身不会引发异常,所以需要自己引发一个:
try:
if(0<= n <= 99):
...
else:
raise Exception()
except Exception:
...
当您的条件不满足时,您需要从 fizzbuzz() 引发异常。尝试以下:
if __name__ == '__main__':
def fizzbuzz(n):
try:
if(0<= n <= 99):
for i in range(n):
if i==0:
print("0")
elif (i%3==0 and i%7==0) :
print("fizzbuzz")
elif i%3==0:
print("fizz")
elif i%7==0:
print("buzz")
else:
print(i)
else:
raise ValueError("Your exception message")
except Exception:
print("/// ATTENTION:The number you entered was not in between 0 and 99///")
try:
enteredNumber = int(input("Please enter a number in between 0 and 99: "))
fizzbuzz(enteredNumber)
except Exception:
print("/// ATTENTION: Something went wrong here. Next time, try to enter a valid Integer ////")
此外,您必须捕获特定异常而不是捕获一般异常。
我是 Python 的新手,在我设法学习 Java 之后达到了高级水平。 在 Java 中,使用异常处理进行输入验证对我来说从来都不是问题,但在 python 中,我有点困惑:
这是一个简单的 FizzBuzz 程序示例,它只能读取 0 到 99 之间的数字,否则必须抛出异常:
if __name__ == '__main__':
def fizzbuzz(n):
try:
if(0<= n <= 99):
for i in range(n):
if i==0:
print("0")
elif (i%3==0 and i%7==0) :
print("fizzbuzz")
elif i%3==0:
print("fizz")
elif i%7==0:
print("buzz")
else:
print(i)
except Exception:
print("/// ATTENTION:The number you entered was not in between 0 and 99///")
try:
enteredNumber = int(input("Please enter a number in between 0 and 99: "))
fizzbuzz(enteredNumber)
except Exception:
print("/// ATTENTION: Something went wrong here. Next time, try to enter a valid Integer ////")
如果我 运行 并输入例如123 代码刚刚终止,没有任何反应。
如果你想捕捉异常,你需要确保它会在期望的场景发生时被引发。
由于try和except之间的代码块本身不会引发异常,所以需要自己引发一个:
try:
if(0<= n <= 99):
...
else:
raise Exception()
except Exception:
...
当您的条件不满足时,您需要从 fizzbuzz() 引发异常。尝试以下:
if __name__ == '__main__':
def fizzbuzz(n):
try:
if(0<= n <= 99):
for i in range(n):
if i==0:
print("0")
elif (i%3==0 and i%7==0) :
print("fizzbuzz")
elif i%3==0:
print("fizz")
elif i%7==0:
print("buzz")
else:
print(i)
else:
raise ValueError("Your exception message")
except Exception:
print("/// ATTENTION:The number you entered was not in between 0 and 99///")
try:
enteredNumber = int(input("Please enter a number in between 0 and 99: "))
fizzbuzz(enteredNumber)
except Exception:
print("/// ATTENTION: Something went wrong here. Next time, try to enter a valid Integer ////")
此外,您必须捕获特定异常而不是捕获一般异常。