I'm not really sure how to define it so that Spin could be equal to the string input of a user.
The issue isn't in the value in String
; the issue is in the Boolean condition you're expressing.
Your while
loop is framed as follows:
while Spin != "Clockwise" or Spin != "Counterclockwise":
print("Please try again!")
Let's walk through the evaluation for arbitrary inputs.
Suppose your user enters Clockwise
.
Spin != "Clockwise"
will evaluate to False
Spin != "Counterclockwise"
is equivalent to "Clockwise" != "Counterclockwise"
, which is True
while False or True
is while True
, so the body of the while
loop will evaluate.
Suppose instead your user enters Counterclockwise
.
Spin != "Clockwise"
will evaluate to True
.
while True or [...]
is while True
, so the body of the while
loop will evaluate (Python evaluates Boolean conditions lazily, so it won't even bother testing the part of the expression to the right of the or
; see here).
If you want to validate the input to ensure it's in one of two values (Clockwise
or Counterclockwise
) you need to check something like:
while not (Spin == "Clockwise" or Spin == "Counterclockwise"):
...
or:
while Spin != "Clockwise" and Spin != "Counterclockwise"):
...
You can express this condition more concisely as:
while Spin not in ("Clockwise", "Counterclockwise"):
...
Better yet, if the set of inputs is known and reasonably fixed, you can write something like this:
from enum import Enum
class Spin(Enum):
CLOCKWISE = "clockwise"
COUNTERCLOCKWISE = "counterclockwise"
def __str__(self):
return self.value
input_spin = input("Enter direction of spin:")
# Loosely normalize input_spin so that "ClockwiSe "
# is treated like a valid input for "clockwise".
while not input_spin.strip().lower() in map(str, Spin):
...