-1

I am trying to build something simple from a book that I have. I want it to only accept one of two inputs, either Clockwise or Counterclockwise. However, the program enters the while loop even while they type Clockwise or Counterclockwise. Is there a way I can fix this?

import turtle

Spin = input("Clockwise or Counterclockwise?")
while Spin != "Clockwise" or Spin != "Counterclockwise":
    print("Please Try Again")
    Spin = {input("Clockwise or Counterclockwise?")}
x = input(str("Put in desired Length:"))
SideLength = int(x)

def drawSideAndTurn(SideLength):
    turtle.forward(SideLength)
    if Spin == "Clockwise":
        turtle.right(90)
    if Spin == "Counterclockwise":
        turtle.left(90)
 y = (input("Number of Sides:"))
 n = int(y)
 x = 1
while x <= n:
    drawSideAndTurn(SideLength)
    drawSideAndTurn(SideLength)
    x += 1
    SideLength += 10
  • 1
    hint: how could `Spin != "Clockwise" or Spin != "Counterclockwise":` ever be false, for any value of `Spin`? (It can't.) – Robin Zigmond Mar 12 '21 at 23:54
  • If `Spin != "Clockwise"` is false, then `Spin != "Counterclockwise"` must be true, and visa-versa. – Scott Hunter Mar 12 '21 at 23:55
  • I'm new to python so I'm not really sure how to define it so that Spin could be equal to the string input of a user. – RuSXSnIpERXx Mar 12 '21 at 23:56
  • Use `while Spin not in ["Clockwise", "Counterclockwise"]:` – jarmod Mar 12 '21 at 23:57
  • 1
    @RuSXSnIpERXx if `Spin` is `"Clockwise"`, then it's not `"Counterclockwise"`, so the `!= "Counterclockwise"` part of the `if` statement will return `True`, meaning it's never possible for the loop to end. You should do `and` instead of `or`, or use the `not in` example above. – Random Davis Mar 12 '21 at 23:58
  • 1
    There are only two options, it is either Clockwise or Counterclockwise. So by making the condition Spin!="Clockwise" or Spin!="Counterclockwise", it will always evaluate to be true. That's like flipping a coin and saying "I win if it's not heads OR it's not tails". That will always be true. – Boskosnitch Mar 12 '21 at 23:58

2 Answers2

0

Just change “or” to “and”. The input can’t be both be “clockwise” and “counter clockwise” at the same time, that’s why it never exits the while loop.

0

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.

  1. Spin != "Clockwise" will evaluate to False

  2. Spin != "Counterclockwise" is equivalent to "Clockwise" != "Counterclockwise", which is True

  3. while False or True is while True, so the body of the while loop will evaluate.

Suppose instead your user enters Counterclockwise.

  1. Spin != "Clockwise" will evaluate to True.

  2. 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):
    ...
crcvd
  • 1,465
  • 1
  • 12
  • 23