【发布时间】:2019-10-26 18:44:31
【问题描述】:
我想获得大于零的用户输入。如果用户输入的不是正整数,则再次提示他该消息,直到他输入为止。
x = input("Enter a number:")
y= int(x)
while y<0:
input("Enter a number:")
if not y<0:
break
如果我先添加一个坏值,然后添加一个好值,它只会不断要求一个新值。就像 Try -2 它再次询问,但我给 2 并且它仍然在询问。为什么?
【问题讨论】:
-
Python: How to keep repeating a program until a specific input is obtained? 的可能重复项还有
y = int(input("Enter a number:"))您需要将input()返回值分配给变量
标签: python python-3.x input while-loop