-1

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
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
Rooney
  • 1
  • 1
  • 2
    Why do you want to count the number of failed attempts? That doesn't seem to be a requirement in the quoted question. Aside from that, this question seems a duplicate of [Asking the user for input until they give a valid response](http://stackoverflow.com/q/23294658/198633) – inspectorG4dget Apr 13 '15 at 02:08

3 Answers3

4

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
>>> 
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
2

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()
Ajay
  • 5,267
  • 2
  • 23
  • 30
0

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
samlexxy
  • 115
  • 1
  • 1
  • 8