【发布时间】:2018-12-01 21:39:58
【问题描述】:
我正在处理一个需要用户输入的小型编码挑战。应检查此输入是否为数字。我创建了一个“try: ... except ValueError: ...”块,它检查一次输入是否为数字,但不是多次。我希望它基本上可以连续检查。
可以创建一个while异常循环吗?
我的代码如下:
try:
uinput = int(input("Please enter a number: "))
while uinput <= 0:
uinput = int(input("Number is negative. Please try again: "))
else:
for i in range(2, uinput):
if (uinput % i == 0):
print("Your number is a composite number with more than
one divisors other than itself and one.")
break
else:
print(uinput, "is a prime number!")
break
except ValueError:
uinput = int(input("You entered not a digit. Please try again: "))
【问题讨论】:
标签: python python-3.x while-loop exception-handling