Python Guessing Game Correct answer being printed even when player gets answer right
Question: So I have recently started programming python and I have one issue with this
code. When the player gets the answer incorrect after using all their lives it
should print the answer which it does but only the first time the layer plays
i they play again they don't get told the correct answer if the get it wrong.
It also does this when the player gets the answer correct. Plus the number the
computer chooses stays the same when you use the play again function. Please
try and help me but bear in mind my understanding cane be very limited in some
aspects of python. I've included lots of comments to help others understand
whats going on. I have included my code and what I get in the shell.

Code:

    
    
    #imports required modules
    import random
    from time import sleep
    
    #correct number variable created
    num = 0
    #generates number at random
    comp_num = random.randint(1,10)
    print('I\'m thinking of a number guess what it is...\n')
    
    #main game code
    def main():
        #lives created
        lives = 3
        #correct number variable reset
        num = 0
        while lives >= 1:
            #player guesses
            guess = int(input('Guess: '))
            if comp_num == guess:
                #if correct says well done
                input('\nWell Done! You guessed Correctly!\n')
                #player doesn't get told what the number is if there right
                num = num +1
                break
            elif comp_num >= guess:
                #if guess is too low tells player
                #one live taken for incorrect guess
                lives = lives -1
                print('\nToo low!\n')
                #player is told how many lives they have left
                print('You guessed incorrectly. You have',lives,'live(s) remaining.\n')
            elif comp_num <= guess:
                #if guess is too high tells player
                #one live taken for incorrect guess
                lives = lives -1
                print('\nToo high!\n')
                #player is told how many lives they have left
                print('You guessed incorrectly. You have',lives,'live(s) remaining.\n')
    
    def end():
        #asks player if they want to play again
        play_again = input('Would you like to play again?[Y/N] ')
        while play_again.lower() == 'y':
            #if they do game resets and plays again
            if play_again.lower() == 'y':
                comp_num = random.randint(1,10)
                print('\nI\'m thinking of a number guess what it is...\n')
                main()
                play_again = input('Would you like to play again?[Y/N] ')
                if play_again.lower() == 'n':
                    break
        if play_again.lower() == 'n':
            #if they don't game ends
            input('\nOk, Press enter to exit')
            exit()
    
    main()
    
    if num != 1:
            #if player guesses incorrectly they get told the correct awnser
            print('The number I was thinking of was...',comp_num,'!\n')
    
    end()
    

SHELL:

    
    
    I'm thinking of a number guess what it is...
    
    Guess: 5
    
    Well Done! You guessed Correctly!
    
    The number I was thinking of was... 5 !
    
    Would you like to play again?[Y/N] y
    
    I'm thinking of a number guess what it is...
    
    Guess: 5
    
    Well Done! You guessed Correctly!
    
    Would you like to play again?[Y/N] y
    
    I'm thinking of a number guess what it is...
    
    Guess: 5
    
    Well Done! You guessed Correctly!
    
    Would you like to play again?[Y/N] y
    
    I'm thinking of a number guess what it is...
    
    Guess: 5
    
    Well Done! You guessed Correctly!
    


Answer: The problem with your function is that you have a global variable named `num`,
but your `main` function also has a local variable named `num`. The `num += 1`
line inside `main` only changes the local variable. But the `if num != 1` at
the end checks the global variable.

To fix this, add a global statement:

    
    
    def main():
        global num
        # the rest of your code
    

* * *

Why does this work?

In Python, any time you write an assignment statement (like `num = 0` or `num
+= 1`) in a function, that creates a local variable—unless you've explicitly
told it not to, with a `global` statement.* So, adding that `global num` means
that now, there is no local variable `num`, so `num += 1` affects the global
instead.

This is explained in more detail in the tutorial section on [Defining
Functions](https://docs.python.org/3/tutorial/controlflow.html#defining-
functions).

* Or a `nonlocal` statement, but you don't want to learn about that yet.

* * *

However, there's a better way to fix this. Instead of using a global variable,
you can `return` the value in the local variable. Like this:

    
    
    def main():
        # your existing code
        return num
    
    # your other functions
    
    score = main()
    if score != 1:
        #if player guesses incorrectly they get told the correct awnser
        print('The number I was thinking of was...',comp_num,'!\n')
    

