0
y=''
print 'y= nothing'
y = raw_input('-->')
while y!='O' or y!='X':
    print 'You have to choose either X or O'
    y = raw_input('-->')
print 'Loop exited'
print y

Could anyone explain why the above code doesn't run properly in python?

I assume that any time the user inputs something except 'X' or 'O' he gets the message and the prompt for input. But as soon as the user provides either 'X' or 'O', the loop should exit. But it doesn't... Could you, guys help? I am a novice in python...

4
  • 1
    Use and instead of or for your while loop. Commented Sep 15, 2017 at 6:06
  • 4
    Your condition is the same as not (y=='O' and y=='X'), y cannot equals to two different char. So y=='O' and y=='X' is always False, and then not (y=='O' and y=='X')is always True. I think what you want is y!='O' and y!='X'. Commented Sep 15, 2017 at 6:06
  • @Serjik that logic is not the same as what the author wants. I think OP wants to not enter the loop when y is equal to 'O' or 'X'. Commented Sep 15, 2017 at 6:25
  • @Jerrybibo Exactly, you got it !))) Sorry if my explanation was not that clear. I want not to enter the loop if Y = X or Y = O. Commented Sep 15, 2017 at 20:09

3 Answers 3

2

There are several fixes to this erroneous logic flow. One was already mentioned by @Aanchal Sharma by having two !='s and an and within the while loop like so:

while y != 'O' and y != 'X':

An alternate solution is to use in which I personally find more readable:

while y not in ['X', 'O']:
    print 'You have to choose either X or O'
    y = raw_input('--> ')

Hope this helped!

Sign up to request clarification or add additional context in comments.

Comments

1

A different way of doing this is to use a while True and a break statement:

while True:
    y = raw_input('-->')

    if  y in ['O', 'X']:
        break
    print 'You have to choose either X or O'

print 'Loop exited'
print y

For example:

-->a
You have to choose either X or O
-->b
You have to choose either X or O
-->c
You have to choose either X or O
-->O
Loop exited
O

This avoids needing to have two input statements.

1 Comment

Thank you ) I was blind writing it) You are 100% right)
1
y=''
print 'y= nothing'
y = raw_input('-->')
while (y!='O' and y!='X'):
    print 'You have to choose either X or O'
    y = raw_input('-->')
print 'Loop exited'
print y

Use 'and' condition not 'or'. In your case, y will always be either not-equal to 'O' or not-equal to 'X'. It can't be equal to both at the same time.

3 Comments

If y is 'O', then the loop should exit because it says 'You have to choose either X or O'. The loop is supposed to continue only till y is not 'X' or 'O'.
@anuragal I think you are misunderstanding what author wants.
@AanchalSharma Thank you. You are right. In my erroneous piece I always had Y !=1 or Y !=2 :) that was funny. What to say....newbie:) Appreciate your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.