Fuji
Fuji

Reputation: 41

How do I keep track of the number of inputs by a user, and display them in a string?

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 struggling to determine how to keep track of the number of times a user guesses before they reach the correct number, and display that number in a string. I currently have a while loop in the function main():

def main(random_int, new_guess, user_attempts): #Function that holds the main algorithim and calls all of the functions in the program
    r = random_int(size) #Setting parameters to generate a random number between 1 - 1000
    n = new_guess() #Assigns the user's input as "guess", and calls the function "new_guess"
    
    while n != r: #While loop to continue until user guesses correct number
        if n > r:
            print("The number you guessed is too high, guess again.")
        elif n < r:         
            print("The number you guessed is too low, guess again.")
        attempts =+ 1
        n = new_guess()
        
    if n == r: #If user guesses correct number, call "win" function
        win(random_int, new_guess, user_attempts)

And am attempting to take the value of attempts, and store it in the function user_attempts(): where I can call it in the function win():. I keep getting the error - TypeError: 'int' object is not callable

The full program for context:

#Python number guessing game

#Import randrange module
from random import randrange

#Initialize variables
attempts = 0
size = 1000

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 user_attempts():
    a = attempts
    return a

def win(random_int, new_guess, user_attempts): #Prints that the answer is correct, along with the number of guesses it took 
    random_int = random_int(size)
    a = user_attempts()
    
    if a >= 2: #If it took the user more than 1 attempt, uses "guesses" for proper grammar
        print("You guessed the correct number", str(random_int()), ", you win! It took you ", str(user_attempts()), " guesses.")
    elif a < 2: #If it took the user only 1 attempt, uses "guess" for proper grammar
        print("You guessed the correct number", str(random_int()), ", you win! It took you ", str(user_attempts()), " guess.") 

def main(random_int, new_guess, user_attempts): #Function that holds the main algorithim and calls all of the functions in the program
    r = random_int(size) #Setting parameters to generate a random number between 1 - 1000
    n = new_guess() #Assigns the user's input as "guess", and calls the function "new_guess"
    
    while n != r: #While loop to continue until user guesses correct number
        if n > r:
            print("The number you guessed is too high, guess again.")
        elif n < r:         
            print("The number you guessed is too low, guess again.")
        attempts =+ 1
        n = new_guess()
        
    if n == r: #If user guesses correct number, call "win" function
        win(random_int, new_guess, user_attempts)

main(random_int, new_guess, user_attempts) #Calls the "main" function, runs the program

Upvotes: 0

Views: 681

Answers (1)

axby
axby

Reputation: 323

For your "int object is not callable" error, that means you are calling an integer variable like it is a function. I think it's where you do:

    random_int = random_int(size)

The first call to random_int may work if it's a function, but then you're assigning a local variable called random_int the return value of that function, which is an integer. So the next time you call random_int, it's an integer instead of a function. To fix it, you should use a different variable name besides random_int, since that is what you called your function.

+= vs =+

It looks like you have:

   attempts =+ 1

But you probably mean to swap the + and =:

    attempts += 1

attempts =+ 1 is like attempts = +1, where + is unary positive (like unary negative).

Upvotes: 1

Related Questions