Home > other >  How do I compare the input value from one function, to the return value of another function?
How do I compare the input value from one function, to the return value of another function?

Time:09-30

I have a class project, where I am making a number guessing game. I have the following requirements:

#1. A main() function that holds the primary algorithm, but itself only passes information among other functions. main() must have the caller for random_int()
#2. A function called in main() (not nested in main()!) that compares the user's guess to the number from random_int() and lets the user know if it was too high or too low.
#3. A function called in main() that asks the user for a new guess.
#4. A function that prints out a string letting the user know that they won.
#5. Tell the user how many guesses it took them to get the correct answer.

I am currently having an issue trying to take the user inputted value "guess" and compare it with the value of a randomly generated integer "random_int" in a while loop in the function def high_low():

def random_int(size): #Generates a random integer from given parameters (size)
    return randrange(1, size 1)

def new_guess(): #Prompts the user to enter an integer as their guess
    guess = (input("Enter your guess (between 1 - 1000): "))
    return guess
    
def high_low(random_int, new_guess): #Lets the user know if the number they guessed is too high or too low
    while guess != random_int: #While loop to continue until user guesses correct number
        if guess > random_int:
            print("The number you guessed is too high, guess again.")
        elif guess < random_int:
            print("The number you guessed is too low, guess again.")
        attempts =1

I either get the error "guess not defined" or '>' not supported between instances of 'function' and 'function'

Here is all of the code for context, note though that most of it below what I have posted above is pseudocode for the purposes of figuring out the logic of the game's function, and I have not yet gone through with debugging.

#Python number guessing game

#Import randrange module
from random import randrange

#Initialize variables
attempts = 0

def random_int(size): #Generates a random integer from given parameters (size)
    return randrange(1, size 1)

def new_guess(): #Prompts the user to enter an integer as their guess
    guess = (input("Enter your guess (between 1 - 1000): "))
    return guess

def high_low(random_int, new_guess): #Lets the user know if the number they guessed is too high or too low
    while guess != random_int: #While loop to continue until user guesses correct number
        if guess > random_int:
            print("The number you guessed is too high, guess again.")
        elif guess < random_int:
            print("The number you guessed is too low, guess again.")
        attempts =1
        new_guess()

def win(random_int, new_guess): #Prints that the answer is correct, along with the number of guesses it took
    while guess == random_int: 
        if attempts >= 2: #If it took the user more than 1 attempt, uses "guesses" for proper grammar
            print("You guessed the correct number, you win! It took you ", str(attempts()), " guesses.")
            input("Would you like to play again? (Y/N): ")
            if input == Y: #If user inputs "Y", runs the program again
                main()
            elif input == N: #If user inputs "N", terminates the program
                break
        elif attempts < 2: #If it took the user only 1 attempt, uses "guess" for proper grammar
            print("You guessed the correct number, you win! It took you ", str(attempts()), " guess.") 
            input("Would you like to play again? (Y/N): ") 
            if input == Y: #If user inputs "Y", runs the program again
                main()
            elif input == N: #If user inputs "N", terminates the program
                break
                        
def main(): #Function to call all functions in the program
    random_int(1000)
    new_guess()
    high_low(random, new_guess)
    win()
    
main() #Calls the "main" function, runs the program

CodePudding user response:

The code has a couple of issues I'll walk through all of them with an explanation so that we understand the reason why they happen at all. First we'll address all errors one by one.

Error-1 The first error on executing the code is '>' not supported between instances of 'function' and 'function'. To understand that, notice the difference between Call-1 and Call-2 in below example code:

def f1():
    return 1

def f2():
    return 2

def less_than(n1, n2):
    return n1 < n2

less_than(f1, f2)     # Call-1: this will not work and give you error similar to what you get
less_than(f1(), f2()) # Call-2: this works

Call-1 passes the function itself, whereas Call-2 passes result of f1() and f2(), which are integers and can be compared by <.

In the code the main() needs to be rewritten like this:

def main(): #Function to call all functions in the program
    r = random_int(1000)
    n = new_guess()
    high_low(r, n)
    win()

Error-2 After above fix, executing will give another error: NameError: name 'guess' is not defined

It means guess has not been defined. That's fixed by re-writing high_low() again like this. Notice the name new_guess replaced with guess. One is the function and other is the variable.

def high_low(random_int, guess): #Lets the user know if the number they guessed is too high or too low
    while guess != random_int: #While loop to continue until user guesses correct number
        if guess > random_int:
            print("The number you guessed is too high, guess again.")
        elif guess < random_int:
            print("The number you guessed is too low, guess again.")
        attempts =1
        guess = new_guess()

Error-3 Again running would give this error: TypeError: '>' not supported between instances of 'str' and 'int' Fix is simple, the new_guess() function needs to convert input to int as calling input returns everything as string.

def new_guess(): #Prompts the user to enter an integer as their guess
    guess = int(input("Enter your guess (between 1 - 1000): "))
    return guess

Error-4 Last error would be: UnboundLocalError: local variable 'attempts' referenced before assignment

This simply means no value has been set to attempts before using it in attempts = 1 This gets fixed again by updating high_low and adding attempts = 0:

def high_low(random_int, guess): #Lets the user know if the number they guessed is too high or too low
    attempts = 0
    while guess != random_int: #While loop to continue until user guesses correct number
        if guess > random_int:
            print("The number you guessed is too high, guess again.")
        elif guess < random_int:
            print("The number you guessed is too low, guess again.")
        attempts =1
        guess = new_guess()

Final code looks like this:

from random import randrange

#Initialize variables
attempts = 0

def random_int(size): #Generates a random integer from given parameters (size)
    return randrange(1, size 1)

def new_guess(): #Prompts the user to enter an integer as their guess
    guess = int(input("Enter your guess (between 1 - 1000): "))
    return guess

def high_low(random_int, guess): #Lets the user know if the number they guessed is too high or too low
    attempts = 0
    while guess != random_int: #While loop to continue until user guesses correct number
        if guess > random_int:
            print("The number you guessed is too high, guess again.")
        elif guess < random_int:
            print("The number you guessed is too low, guess again.")
        attempts =1
        guess = new_guess()

def win(random_int, new_guess): #Prints that the answer is correct, along with the number of guesses it took
    while guess == random_int: 
        if attempts >= 2: #If it took the user more than 1 attempt, uses "guesses" for proper grammar
            print("You guessed the correct number, you win! It took you ", str(attempts()), " guesses.")
            input("Would you like to play again? (Y/N): ")
            if input == Y: #If user inputs "Y", runs the program again
                main()
            elif input == N: #If user inputs "N", terminates the program
                break
        elif attempts < 2: #If it took the user only 1 attempt, uses "guess" for proper grammar
            print("You guessed the correct number, you win! It took you ", str(attempts()), " guess.") 
            input("Would you like to play again? (Y/N): ") 
            if input == Y: #If user inputs "Y", runs the program again
                main()
            elif input == N: #If user inputs "N", terminates the program
                break
                        
def main(): #Function to call all functions in the program
    r = random_int(1000)
    n = new_guess()
    high_low(r, n)
    win()
    
main() #Calls the "main" function, runs the program

CodePudding user response:

your high_low function has no reference to a variable named guess. I think the solution is to just add the line guess = new_guess() right before the while loop.

  • Related