I am using the def in python for the first time and i think i messed up somewhere

hi i was trying to learn to use def for first time and this error came

Traceback (most recent call last):
  File "C:\Users\Hp\PycharmProjects\whack a mole\main.py", line 36, in <module>
    mole_spawn(600, 440)
TypeError: 'int' object is not callable

there are multiple answers to solve this question and i saw many answers but my brain is too small that i couldnt understand what to do and umm we can say i tried but i am just an idiot so nothin went in my brain so sorry dont tell me to try first my brain is so small

code – (only the part of code i think is needed)

def mole_spawn(molepos1, molpos2):
    molepos1 = molepos1 + 67
    molpos2 = molpos2 - 3
mole_spawn = random.randint(1, 9)
mole_spawn2 = random.randint(1, 9)
molepos1 = 0
molepos2 = 0
while mole_spawn == mole_spawn2:
    mole_spawn = random.randint(1, 9)
    mole_spawn2 = random.randint(1, 9)
if mole_spawn == 1:
    mole_spawn(100, 440)
elif mole_spawn == 2:
    mole_spawn(350, 440)
elif mole_spawn == 3:
    mole_spawn(600, 440)
elif mole_spawn == 4:
    mole_spawn(100, 260)
elif mole_spawn == 5:
    mole_spawn(350, 260)
elif mole_spawn == 6:
    mole_spawn(600, 260)
elif mole_spawn == 7:
    mole_spawn(100, 80)
elif mole_spawn == 8:
    mole_spawn(350, 80)
elif mole_spawn == 9:
    mole_spawn(600, 80)
mole_rect = mole.get_rect(center=(molepos1, molepos2))

if anyone helps me i am gonna bless him and he wont get rick rolled for his entire life i promise pls help me:((

>Solution :

def mole_spawn(molepos1, molpos2):
    molepos1 = molepos1 + 67
    molpos2 = molpos2 - 3
mole_spawn = random.randint(1, 9)

This is your code. Here you define variable mole_spawn which shadows and replaces function name mole_spawn . So when you call this function later you actually try to call this int variable, which is not possible, thus error.

Leave a Reply