-1

I have been working on this code:

print('Type any number or letter to start. ')
begin = input()
if begin != "Z" or 'z':
    print("Starting up!")
elif begin == "Z" or "z":
    print("Except Z")

When I run it, I expect it to be that when I type "Z" for the input, it prints the message "Except Z". Instead, it prints "Starting up!" which seems to be very counter-intuitive. Can anyone find where my problem is?

1 Answers1

1

There are a couple of ways to phrase what you mean in Python.

print('Type any number or letter to start. ')
begin = input()
if begin == "z" or begin == "Z":
    print("Except Z")
else:
    print("Starting up!")

is the first;

print('Type any number or letter to start. ')
begin = input()
if begin in ("z", "Z"):
    print("Except Z")
else:
    print("Starting up!")

is another idiomatic way.

The problem in your original code, anyway, is that or has a lower precedence, so as far as Python is concerned, it ends up as

(or
   (begin != "Z")
   ("z")
)

and "z" is always truthy.

AKX
  • 152,115
  • 15
  • 115
  • 172