I was trying to learn to use def for first time and this error came
Traceback (most recent call last):
File "C:UsersHpPycharmProjectswhack a molemain.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 I couldn’t understand what to do.
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))
What can I do to fix this?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
“def” keyword is used to define a function in python. You used same name for function and variable (mole_spawn) which is causing error.
I suggest you to modify
mole_spawn = random.randint(1, 9) # to mole_spawn1 = random.randint(1, 9) while mole_spawn == mole_spawn2: # to while mole_spawn1 == mole_spawn2:
and finally modify mole_spawn to mole_spawn1 in every if else statement.
This should work fine.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0