Python 如何";在else语句“之后从开始运行此程序?”;?
如果用户输入了一个代码之外的数字,那么我想在continuation中从头开始再次运行程序Python 如何";在else语句“之后从开始运行此程序?”;?,python,python-3.x,Python,Python 3.x,如果用户输入了一个代码之外的数字,那么我想在continuation中从头开始再次运行程序 number = input("Enter your number up to 100 : ") while int(number) < 100: print(number) else: print("\n**The number is outside the Limit.**") number=input(“最多输入100:”) 当int(number)
number = input("Enter your number up to 100 : ")
while int(number) < 100:
print(number)
else:
print("\n**The number is outside the Limit.**")
number=input(“最多输入100:”)
当int(number)<100时:
打印(数字)
其他:
打印(“\n**数字超出限制。**”)
您确定要在代码中使用“while”吗?我不确定你需要什么,但是考虑一下这个:
while True:
number = input("Enter your number up to 100 : ")
if int(number) < 100:
print(number)
break
else:
print("\n**The number is outside the Limit.**")
为True时:
数字=输入(“输入最多100的数字:”)
如果int(数字)<100:
打印(数字)
打破
其他:
打印(“\n**数字超出限制。**”)
您确定要在代码中使用“while”吗?我不确定你需要什么,但是考虑一下这个:
while True:
number = input("Enter your number up to 100 : ")
if int(number) < 100:
print(number)
break
else:
print("\n**The number is outside the Limit.**")
为True时:
数字=输入(“输入最多100的数字:”)
如果int(数字)<100:
打印(数字)
打破
其他:
打印(“\n**数字超出限制。**”)
这可能是最简单的:
while True:
number = input("Enter your number up to 100 : ")
if int(number) < 100:
print(number)
break
print("\n**The number is outside the Limit.**")
为True时:
数字=输入(“输入最多100的数字:”)
如果int(数字)<100:
打印(数字)
打破
打印(“\n**数字超出限制。**”)
但是如果用户输入一个非数字的值呢?尝试将字符串转换为int时将引发异常。因此:
while True:
number = input("Enter your number up to 100 : ")
try:
if int(number) < 100:
print(number)
break
print("\n**The number is outside the Limit.**")
except ValueError:
print("You did not enter a number")
为True时:
数字=输入(“输入最多100的数字:”)
尝试:
如果int(数字)<100:
打印(数字)
打破
打印(“\n**数字超出限制。**”)
除值错误外:
打印(“您没有输入数字”)
这可能是最简单的:
while True:
number = input("Enter your number up to 100 : ")
if int(number) < 100:
print(number)
break
print("\n**The number is outside the Limit.**")
为True时:
数字=输入(“输入最多100的数字:”)
如果int(数字)<100:
打印(数字)
打破
打印(“\n**数字超出限制。**”)
但是如果用户输入一个非数字的值呢?尝试将字符串转换为int时将引发异常。因此:
while True:
number = input("Enter your number up to 100 : ")
try:
if int(number) < 100:
print(number)
break
print("\n**The number is outside the Limit.**")
except ValueError:
print("You did not enter a number")
为True时:
数字=输入(“输入最多100的数字:”)
尝试:
如果int(数字)<100:
打印(数字)
打破
打印(“\n**数字超出限制。**”)
除值错误外:
打印(“您没有输入数字”)
我假设您希望发出第一个提示,然后继续接受用户输入,除非超出限制,在这种情况下,再次打印警告和第一个提示
在这种情况下,您需要从第一个提示中单独收集输入:
while True:
print("Enter your number up to 100:")
while True:
number = int(input())
if number < 100:
print("number is:", number)
else:
print("number is outside the limit")
break
当然,我可能没有正确解释您的问题,但我希望这会有所帮助。我假设您希望发出第一个提示,然后继续接受用户输入,除非超出限制,在这种情况下,再次打印警告和第一个提示 在这种情况下,您需要从第一个提示中单独收集输入:
while True:
print("Enter your number up to 100:")
while True:
number = int(input())
if number < 100:
print("number is:", number)
else:
print("number is outside the limit")
break
当然,我可能没有正确地解释您的问题,但我希望这会有所帮助。将number=input(“输入您的数字,最多100:”)
放入循环中。将孤立的else
更改为if
Putnumber=input(“最多输入100:”)
。将孤立的else
更改为if