Sep-14-2023, 04:56 PM
So I just started learning Python a few days ago and have written a very simple block of code, but it doesn't do what I think it would. Can you please walk me through what is actually happening here while it's checking the conditions?
I would expect this to work the way it does in C++, where if I were to type:
So what is happening in the python code?? I don't want to have to type out "if player != 'a', and player != 'b', and player != 'c', etc.
(I am not really looking to fix my code, but rather for an explanation as to why my code is incorrect, and how it's actually being read/interpreted in Python) Thank you.
while True: player = input() if player != ('a' or 'b' or 'c' or 'd'): print('whatever') else: breakSo, what I would expect to happen is that if the player inputs anything other than a, b, c, or d, then it will print the text. And if they input one of a, b, c, d, then it will break the loop. But what ends up happening is if player inputs "a" it will break the loop, and neither b, c, or d will break the loop, but will instead send the text.
I would expect this to work the way it does in C++, where if I were to type:
while (true) { string player = input(); if (player != (a || b || c || d)) { printf("whatever"); } else { break; } }it would have the exact result I'm looking for.
So what is happening in the python code?? I don't want to have to type out "if player != 'a', and player != 'b', and player != 'c', etc.
(I am not really looking to fix my code, but rather for an explanation as to why my code is incorrect, and how it's actually being read/interpreted in Python) Thank you.