i was running some code and this error came after i hit the mole 2 times pls help me get a solution
what this basicly does is after i hit mole it gors anywhere randomely after i hit it 2 times this error comes to understand it more i have posted a video so u can see what is happening
link is https://youtu.be/Lga4SUVQtfc
Traceback (most recent call last): File "C:\Users\Hp\PycharmProjects\whack a mole\main.py", line 74, in <module> random = random.choice(random_locations) AttributeError: 'tuple' object has no attribute 'choice'
code idk where the error is in the code so i am givin all the code
import pygame
from sys import exit
import random
molepos1 = 0
molepos2 = 0
def mole_spawn_new(pos1, pos2):
global final_pos
final_pos = (pos1 + 67)
global final_pos_2
final_pos_2 = (pos2 - 3)
global mole_rect
mole_rect = mole.get_rect(center=(final_pos, final_pos_2))
screen.blit(mole, mole_rect)
return final_pos, final_pos_2
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption(' Whack a mole')
icon = pygame.image.load('whack-a-mole-icon.png')
pygame.display.set_icon(icon)
# the screen
screen.fill((0, 255, 0))
background = pygame.image.load('green-grass-background-2036900.jpg').convert_alpha()
# everything releated to the mole with the mole of course
mole = pygame.image.load('small_mole.png')
mole_home = pygame.image.load('land.png')
mole_home = pygame.transform.scale(mole_home, (130, 80))
mole_spawn = random.randint(1, 9)
mole_spawn2 = random.randint(1, 9)
while mole_spawn == mole_spawn2:
mole_spawn = random.randint(1, 9)
mole_spawn2 = random.randint(1, 9)
if mole_spawn == 1:
mole_spawn_new(100, 440)
elif mole_spawn == 2:
mole_spawn_new(350, 440)
elif mole_spawn == 3:
mole_spawn_new(600, 440)
elif mole_spawn == 4:
mole_spawn_new(100, 260)
elif mole_spawn == 5:
mole_spawn_new(350, 260)
elif mole_spawn == 6:
mole_spawn_new(600, 260)
elif mole_spawn == 7:
mole_spawn_new(100, 80)
elif mole_spawn == 8:
mole_spawn_new(350, 80)
elif mole_spawn == 9:
mole_spawn_new(600, 80)
# hammer
hammer = pygame.image.load('hammer.png')
hammer2 = pygame.image.load('hammer2.png')
hammer3 = pygame.image.load('hammer3.png')
whyamiahammer = hammer2
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
click = pygame.mouse.get_pressed()
if event.button == 1:
whyamiahammer = hammer3
if pygame.Rect.colliderect(hammer_rect, mole_rect):
random_locations = [(100, 440), (350, 440), (600, 440), (100, 260), (350, 260), (600, 260), (100, 80),
(350, 80), (600, 80)]
random = random.choice(random_locations)
print(random[0])
print(random[1])
mole_spawn_new(random[0],random[1])
if event.type == pygame.MOUSEBUTTONUP:
whyamiahammer = hammer2
hammerx, hammery = pygame.mouse.get_pos()
hammer_rect = hammer3.get_rect(center=(hammerx, hammery))
screen.blit(background, (0, 0))
screen.blit(mole_home, (100, 440))
screen.blit(mole_home, (350, 440))
screen.blit(mole_home, (600, 440))
screen.blit(mole_home, (100, 260))
screen.blit(mole_home, (350, 260))
screen.blit(mole_home, (600, 260))
screen.blit(mole_home, (100, 80))
screen.blit(mole_home, (350, 80))
screen.blit(mole_home, (600, 80))
screen.blit(mole, mole_rect)
screen.blit(whyamiahammer, ((hammerx) - 61, (hammery - 73)))
pygame.display.update()
i have tried to see what type of error this is and saw some answers but idk what to do so pls help me find out why this error is coming
>Solution :
The problem is that you are using the name random for a variable, random is a module. Once you have a variable with the same name, you can no longer use the module because the variable will shadows the module. Change the name of the variable:
location = random.choice(random_locations)
print(location[0])
print(location[1])
mole_spawn_new(location[0], location[1])