0

I'm trying to get this game to run ad infinitum until the user enters "q" or "quit". For some reason, this isn't quite working out with what I have. I initially tried to do this with two functions but failed miserably, so I consolidated for right now into one function to try to get it going. Still failing, but not as bad.

#Ex9: Guessing game. Generate a number between 1-9 and provide info whether its too high or low

import random 
#print("Type q or quit to quit") 
#guess = int(input("Please enter a number between 1 to 9: ")) 

def guessing_function():  
    while True: 
        quit1 = print("Type q or quit to quit")
        guess = int(input("Please enter a number between 1 to 9: ")) 
        value = random.randint(1,9)
        if quit1 == "quit".lower() or "q".lower():
            print("Ok we will stop") 
            break 
        elif guess > value: 
            print(f"The computer generated {value}") 
            print(f"You picked {guess}. That value is too high")
        elif guess < value: 
            print(f"The computer generated {value}")
            print(f"You picked {guess}. That value is too low")
        elif guess == value:
            print(f"The computer generated {value}")
            print(f"You picked {guess}. That value is correct!")

          


guessing_function()

Basically the issue is that the script just stops after one loop, rather than continue on without the user's input.... Not sure what I'm doing wrong.

J. do
  • 53
  • 4
  • Your if statement is checking for `if quit1 == "quit".lower() or "q".lower():` first part but the second part is always be True. – Joe Ferndz Mar 20 '21 at 02:34
  • There are at least three issues here: the linked duplicate, a typo (you call `print` in one place where you clearly intended to call `input`), and a logical issue (if you want to stop immediately when `quit` is typed, then you should check whether `quit` was typed *before* prompting for a number). – Karl Knechtel Mar 20 '21 at 03:10

2 Answers2

1

Looks like you want to check if the user wanted to quit.

checking "quit".lower() is not correct. "quit" is already lower.

Also the or part of the if statement is incorrect. It will always be True.

Instead you want to check if lowercase of quit1 is either "quit" or "q".

You can do that by giving if quit1.lower() in ("quit","q"). By using in ("quit","q"), the if statement checks against the items in the tuple. This is a much better way to check when you have multiple values to check.

def guessing_function():  
    while True: 
        quit1 = print("Type q or quit to quit")
        guess = int(input("Please enter a number between 1 to 9: ")) 
        value = random.randint(1,9)

        if quit1.lower() in ("quit","q"): #fixed this line

            print("Ok we will stop") 
            break 
        elif guess > value: 
            print(f"The computer generated {value}") 
            print(f"You picked {guess}. That value is too high")
        elif guess < value: 
            print(f"The computer generated {value}")
            print(f"You picked {guess}. That value is too low")
        elif guess == value:
            print(f"The computer generated {value}")
            print(f"You picked {guess}. That value is correct!")
Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
0

print doesn't prompt for user input(and returns None always), you should replace it with input instead.

The if is also wrong, it should be if quit1 == "quit" or quit1 == "q":. How to test multiple variables against a value? explains it better than I could.

SuperStormer
  • 4,997
  • 5
  • 25
  • 35