0

Is this the correct usage of or in python: if choice == 'N' or 'n':

Or should it be if choice == 'N' or choice == 'n':

devolskii
  • 27
  • 7
  • 1
    Definitely the second. – Mark Jul 11 '20 at 15:57
  • @MarkMeyer I rewrote the line as ```if choice == 'N' ``` and input ```n``` and it still came true. Does is mean that this ```==``` checks the value as case insenitive? – devolskii Jul 11 '20 at 16:08
  • I'm not sure what you are asking @Debol. But comparisons *are* case sensitive. Your first code example will *always* return true — that's the problem with it. – Mark Jul 11 '20 at 16:12
  • @MarkMeyer Yes I just noticed that. Why does this happen? How can I change it being always true? – devolskii Jul 11 '20 at 16:16
  • That the way `or` works in python — it returns the first truthy value it sees. The string `n` is truthy. You can verify with `print(choice == 'N' or 'n')`. See here for better explanation: https://stackoverflow.com/questions/47007680/how-do-and-and-or-act-with-non-boolean-values – Mark Jul 11 '20 at 16:19
  • @MarkMeyer Even for ```if choice == 'N'``` it's returning true. Is there any way to change it? – devolskii Jul 11 '20 at 16:45
  • You must have something else wrong. If you set `choice` to `n`. `choice == 'N'` is certainly false. – Mark Jul 11 '20 at 16:49

0 Answers0