0

So I am going back to basics with Python, after a long stint in JS. And I just something out which goes against what I thought it would do.

I have the following code:

name = ''
while name != 'your name' or 'your name.':
    print('Please type your name.')
    name = input()
print('Thank you!')

However, when I run the script, and input either 'your name' or 'your name' the script just keeps looping and doesn't come out of the loop.

I am confused.

martineau
  • 119,623
  • 25
  • 170
  • 301
Baba.S
  • 324
  • 2
  • 14

2 Answers2

2

That's because your second condition is just a string 'your name.', which is always true. You need to add name != 'your name.' to the second condition:

while name != 'your name' or name != 'your name.':
Spencer Wieczorek
  • 21,229
  • 7
  • 44
  • 54
0

Here is another way:

while True:
    print('Please type your name.')
    name = input()
    if name.startswith('your name'):
        print('invalid')
    else:
        break

print('Thank you!')
Anton vBR
  • 18,287
  • 5
  • 40
  • 46