0

I'm trying to use while with not and or, but for some reason my code doesn't work.

orientation = ""
while orientation is not "h" or "v":
    print("Type 'h' for horizontal and 'v' for vertical") 
    orientation = input()
    if orientation == "h":
        do_something()
    if orientation == "v":
        do_something()

The expected outcome would be, if I was to type "h" or "v" into the input, do_something() would be called and the while loop would end, but instead, the while loop continues, and repeats. What am I doing wrong?

Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
jakethefake
  • 338
  • 1
  • 3
  • 14

1 Answers1

2

One way to write this is like so:

while orientation not in {"h", "v"}:

Alternatively, since you're already checking for "h" and "v" inside the loop, you could avoid repeating yourself:

while True:
    print("Type 'h' for horizontal and 'v' for vertical") 
    orientation = input()
    if orientation == "h":
        do_something()
        break
    if orientation == "v":
        do_something()
        break

(possibly changing the second if into an elif and optionally adding an else clause telling the user their input wasn't recognised).

NPE
  • 486,780
  • 108
  • 951
  • 1,012