'Function printed twice

I'm not sure why my program is printing twice the answer key function I made. i tried adding the else statement down with the function but it kept having some errors the code works fine as how i made it but the function getting printed twice is bothering me.

import random

def main():
    guesses = []
    answerKey(guesses)
    brain(guesses)

def answerKey(guesses):
        print("Well done!")
        print("It took you %i guesses." % len(guesses))
        print("Here are your guesses: ")
        print(guesses)
        
def brain(guesses):
    secretNumber = random.randint(1, 100)
    playerNum = int(input("Enter a number between 1-100: "))
    guesses.append(playerNum)

    while playerNum != secretNumber:
        if playerNum > secretNumber:
            print("Too High!")
        else:
            print("Too Low!")
        playerNum = int(input("Enter a number between 1-100: "))
        guesses.append(playerNum)

    else:
        if playerNum == secretNumber:
            print(answerKey)

    
main()
        


Solution 1:[1]

You should remove the answerKey(guesses) from your main function, since it makes no sense to try to print the guesses before the user has made any.

In your brain function, change:

    else:
        if playerNum == secretNumber:
            print(answerKey)

to:

    answerKey(guesses)

You need to pass guesses to the answerKey function for it to be able to print the right output. The else and if are both unnecessary because you haven't provided any way to exit the loop other than by getting the right answer.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1