Home > Software design >  My code isn't printing any of the while loop when I give it valid input. How do I fix?
My code isn't printing any of the while loop when I give it valid input. How do I fix?

Time:12-13

Trying to run:

pin = int(input('Enter your PIN: '))

while pin != 1234:
  pin = int(input('Incorrect PIN. Enter your PIN again: '))
  
  if pin == 1234:
    print('PIN accepted!')

It just lets me input a code in the console but then doesn't show any sort of output response if I input 1234. If I input the wrong code it does run the while pin loop. How do I get it to run the if pin statment?

I've tried to switch the code around, but I get alot of errors if I do that. Tried restarting visula studio, but that didn't work either. I know the answer has gotta be simple but I don't know enough to find it on my own.

CodePudding user response:

Move the if statement outside of the while loop.

pin = int(input('Enter your PIN: '))

while pin != 1234:
  pin = int(input('Incorrect PIN. Enter your PIN again: '))

if pin == 1234:
  print('PIN accepted!')

The issue is that it only enters the while loop if the pin is not 1234.

  • Related