循环内循环积累?

时间:2015-04-13 02:04:03

标签: python loops while-loop accumulator

如何修改我的程序以便更好地工作,以便它可以遵循以下问题:编写一个条件循环,它将捕获用户,直到它们输入0到100之间的值。如果用户输入超出此范围的任何数字,必须再次输入值。

这是代码:

value = 0
badvalue = 0
while value < 100:
    value = int(input("Enter a value between 0 and a 100:"))
    value = value + 1
    while badvalue >= 100:
        print("Please re-enter the value")
        badvalue = int(input("Enter a value between 0 and a 100:"))
        badvalue = badvalue + 1

3 个答案:

答案 0 :(得分:4)

尝试使用简单的while not

value = -1
while not (0 < value < 100):
    value = int(input("Enter a value between 0 and a 100: "))

>>> while not (0 < value < 100):
...     value = int(input("Enter a value between 0 and a 100:"))
... 
Enter a value between 0 and a 100: 544
Enter a value between 0 and a 100: 213
Enter a value between 0 and a 100: 21
>>> 

答案 1 :(得分:2)

可能是带有if循环的递归函数

def Enter():
    myinp=int(input("Enter a number between 1 and 100:"))
    if myinp <0 or myinp >100:
        Enter()



Enter()

答案 2 :(得分:0)

也许你应该尝试使用这个...可能有点复杂了

而True:

value = eval(input("INPUT NUMBER BETWEEN 0 and 100 : "))
if value > 0 and value < 100:
    print("GOTCHA")
    break
else:
    continue