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
  • 112,593
  • 23
  • 157
  • 280
Baba.S
  • 194
  • 1
  • 12

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
  • 20,481
  • 7
  • 40
  • 51
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
  • 16,833
  • 3
  • 36
  • 44