Search code examples
pythontry-except

How do I make two functions share a variable?


My assignment is to make a secret number, which is 26, and make a guessing game saying the guess is either "too low" or "too high". I made two functions, int_guess for if the input is an integer and not_int_guess for when the input is not an integer. The problem that i have though is when im counting the amount of guesses, i dont know how to make both functions share a count of how many guesses they inputted.

print("Guess the secret number! Hint: it's an integer between 1 and 100...")
secret_num = 26
guess = int(input("What is your guess? "))
def int_guess(guess):
    count = 0
    while guess != 26:
        if guess > secret_num:
            print("Too high!")
            guess = int(input("What is your guess? "))
            count += 1
        elif guess < secret_num:
            print("Too low!")
            guess = int(input("What is your guess? "))
            count += 1
    else:
        print("You guessed it! It took you", count, "guesses.")
def not_int_guess(guess,count):
    print("Bad input! Try again: ")
    guess = int(input("What is your guess? "))
    while guess != 26:
        if guess > secret_num:
            print("Too high!")
            guess = int(input("What is your guess? "))
        elif guess < secret_num:
            print("Too low!")
            guess = int(input("What is your guess? "))
        else:
            print("You guessed it! It took you", count, "guesses.")
try:
    int_guess(guess)
except:
    not_int_guess(guess,count)

One part of the assignment that i need to have is a try and except, the problem is that the count will reset to zero if the except is used, but i need the count to carry over to the exception case. I tried carrying the "count" variable over to the not_int_guess by placing it like not_int_guess(guess,count) but that doesnt work for a reason i dont understand.


Solution

  • Instead of using two functions, use the try and except within the while loop. That way everything is much neater and more efficient (also good to define functions before any main code):

    def int_guess(secret_num):
        count = 0
        guess = 0 #Just defining it here so everything in the function knows about it
        while guess != secret_num:
            try:
                guess = int(input("What is your guess? "))
            except ValueError as err:
                print("Not a number! Error:", err)
                continue #This will make the program skip anything underneath here!
    
            if guess > secret_num:
                print("Too high!")
                
            elif guess < secret_num:
                print("Too low!")
    
            count += 1 #Adds to count
    
        #This will run after the while loop finishes:
        print("You guessed it! It took you", count, "guesses.") 
    
    
    #Main code:
    
    print("Guess the secret number! Hint: it's an integer between 1 and 100...")
    int_guess(26)
    

    Like this, the function will run until the user has guessed the number no matter what they input, while also keeping count through any errors