Set IF statment to check for specific characters, breaks while loop when using any character instead

Viewed 24

I am new to python. I was making a guess the random number game, and I run into issues when having both int and str as inputs, I want the user to exit the program when pressing Q or q, while the game to check for numbers as well. After hours of googling and rewriting this is what I came up with:

#! Python3
import random
upper = 10
number = random.randint(1, upper)
print(number)  #TODO: THIS LINE FOR TESTING ONLY
print("Guess a number between 1 and {}. Press Q to quit!".format(upper))
total_guesses = 0
guess = 0
while guess != number:
    total_guesses += 1
    guess = input()
    if guess.isalpha():
        if guess == "q" or "Q":
            break
        elif guess != "q" or "Q":
            print("Please type in a valid guess.")
            guess = 0
    if guess.isnumeric():
        guess = int(guess)
        if guess == number:
            print("Good job! You guessed the number in {} tries!.".format(total_guesses))
            break
        if guess < number:
            print("Guess Higher!")
        if guess > number:
            print("Guess lower!")
    else:
        print("Valid inputs only")
        guess = 0

This code ALMOST works as intended; issues I have now is that at line 13 and 14 the loop breaks every time when any letter is typed, even though I set the if statement to only check for Q or q, and I can't understand why this is doing it. Any help is appreciated!

1 Answers
if guess == 'q' or "Q":

The way this line is read by python is -

if guess == "q":

and also -

if "Q":

"Q" is a character, which means it's truthy. if "Q" returns True. Try:

if guess == "q" or guess == "Q":

if you feels that's too much, other options include -

if guess in ["Q", "q"]:

if guess.upper() == "Q":
Related