1

In my code, I have a "would you like to play again" feature. For some reason when I put no, it runs it through the not yes or no loop before stopping the code. Why is it doing this? Here is my code:

  again = input('Would you like to play again?\t')
  while again != ('yes') or ('no'):
    print ('Error code 1: Please try again!')
    again = input('Would you like to play again?\t')
    if again == ('yes') or ('no'):
      break
  if again == ('no'):
    break

Here is my output:

Would you like to play again?   no
Error code 1: Please try again!
Would you like to play again?   no
Paolo
  • 20,112
  • 21
  • 72
  • 113
clinggi5
  • 19
  • 4
  • `again != ('yes') or ('no')` is not the right way to test multiple values. – John Gordon Jan 30 '19 at 02:20
  • Can you please explain why not? The other article did not explain this. Thanks so much! – clinggi5 Jan 30 '19 at 02:27
  • Because Python isn't like English. In English we can say "Is X equal to Y or Z", and it's understood that the intent is to compare X to Y and also X to Z. But in Python, you have to fully spell out each condition: `x == y or x == z`. – John Gordon Jan 30 '19 at 02:29
  • But what exactly is wrong with the statement? Shouldn't any statement that is not yes or no go thru the loop? – clinggi5 Jan 30 '19 at 02:31
  • Oh. That makes sense. Thanks so much for clarifying. – clinggi5 Jan 30 '19 at 02:35
  • And the linked answer does say that, but it's a long answer and that bit is easy to miss. – John Gordon Jan 30 '19 at 02:36
  • I'm stilling getting this:Would you like to play again? no debug Error code 1: Please try again! Would you like to play again? no . Even after chaning my code toif again != ('no') or again != ('yes'): print('debug') while again != ('yes') or again != ('no'): print ('Error code 1: Please try again!') again = input('Would you like to play again?\t') if again == ('yes') or ('no'): break if again == ('no'): break – clinggi5 Jan 30 '19 at 02:45
  • sorry, that's really hard to read. – clinggi5 Jan 30 '19 at 02:47
  • No matter what your input is, `if again != ('no') or again != ('yes')` will __always__ be true. You want `and` instead of `or`. – John Gordon Jan 30 '19 at 04:09

0 Answers0