3

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 Answer 1

3
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":
Sign up to request clarification or add additional context in comments.

1 Comment

I completely forgot about the "truthy" aspect! Thank you so much! the first solution worked, I also had to edit the "guess = 0" on line 14 and replace with "continue" in order to restart the checking process. The code works fine now. tyvm!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.