0

This is what I have so far:

userNum = int(input('How many perfect numbers do you wish to sum? (1-4)'))
while userNum != 1 and userNum != 2 and userNum != 3 and userNum != 4:
    userNum = input('You did not enter a number 1-4! Try again!')
if userNum == 1:
    print('Sum is 6 = 6')
elif userNum == 2:
    print('Sum is 6 + 28 = 34')
elif userNum == 3:
    print('Sum is 6 + 28 + 496 = 530')
elif userNum == 4:
    print('Sum is 6 + 28 + 496 + 8128 = 8658')

If I enter a number that is not 1,2,3, or 4 it does re-prompt but it won't exit the while loop if I then enter one of the specified integers. Any advice?

B. Ptak
  • 45
  • 6

1 Answers1

1

input is returning a string and you are comparing it to an int.

userInput = 0
while userInput not in [1, 2, 3, 4]:
    userInput = int(input('Enter a number'))
Tom
  • 939
  • 5
  • 9
  • The user already converted it an integer. –  Nov 29 '18 at 04:53
  • They converted the input from the first input() call into an integer, but not the subsequent calls to input() inside the while loop. – Tom Nov 29 '18 at 04:55
  • Not a problem. As you find the answer correct, don't forget to accept it. – Tom Nov 29 '18 at 16:06