简体   繁体   中英

loops inside loops accumulation?

How can I modify my program to better work so it can follow this question: Write a conditional loop that will trap the user until they enter a value between 0 and 100. If the user enters any number outside of this range, they must enter a value again.

This is the code:

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

Try using a simple 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
>>> 

May be a recursive function with a if loop

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



Enter()

maybe you should try using this...might be a bit complex tho

while True:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM