Reputation: 3
I am having issues getting my code to not accept numbers higher than the maximum. When I put something higher than 101 it just says "Too big, try again", but it wont let me try again. It takes me out of the loop and I have to restart the function to try again. When input a number smaller than 0 it works perfectly.
def addGrade(grade_list):
myGrades = -1
while 0 > myGrades < 101:
myGrades = int(input('Enter a number between 0 and 100: '))
if myGrades < 0:
print('Too small, try again')
if myGrades > 101:
print('Too big, try again')
grade_list.append(myGrades)
print(grade_list)
return myGrades
Upvotes: 1
Views: 67
Reputation: 135
The problem lies within your while loop:
while 0 > myGrades < 101:
must be:
while 0 < myGrades and myGrades < 101:
here's your code working if you enter 1337 it gets out of the function
def addGrade(grade_list):
myGrades = -1
while True:
myGrades = int(input('Enter a number between 0 and 100: '))
if myGrades == 1337:
break
if myGrades < 0:
print('Too small, try again')
continue
if myGrades > 101:
print('Too big, try again')
continue
grade_list.append(myGrades)
print(grade_list)
return myGrades
Upvotes: 1
Reputation: 571
Yes, as already said the while condition is not correct. That's the way I would write it:
my_grade = 0
while not (0 < my_grade < 101):
my_grade = int(input('Enter number: '))
print 'Ok this is a good value {}'.format(my_grade)
Upvotes: 0