Confusing behaviour when using while and if

Hey everyone. I’m kinda new to coding, so I apologize if the terms I’m using aren’t accurate enough. Right now I’m testing the “while” and “if” conditionals with this code:

name = input('Write your name: ')
print('Hello, ' + name + '.')

welcome = input('Press Y to continue: ')

if welcome == 'Y' or welcome == 'y':
    print('You made it')

while welcome != 'y' or welcome != "Y":
    wrong_entry = input('You must press Y to continue, ' + name + ': ')
    if wrong_entry == 'Y' or wrong_entry == 'y':
        print('You made it')`

The thing is, even when the while’s condition is false, the wrong_entry input is shown in console. I don’t know if I’m doing something wrong (probably!). I’ll upload a pic of the console running the code.
Thanks in advance!

You have a boolean logic problem, not a coding problem :slight_smile:

`welcome’ cannot be both ‘y’ and "Y’, so one of the conditions in the while-expression will always be true. That’s why the while-loop is executing.

When you negate an ‘or’ condition, you have to turn it into an ‘and’ condition. In the if-statement, you want to know whether welcome is either ‘Y’ or ‘y’, but in the while-loop you want to know whether it is any value other than those two.

If you change ‘or’ to ‘and’ in the while-loop’s condition expression, the loop won’t run if welcome is either ‘y’ or ‘Y’. However, you still have a problem, because welcome is never changed in the loop, so the loop will never exit.

1 Like

That makes a lot of sense!
Thanks, Kevin! I really appreciate your help! :slight_smile: