AttributeError: 'tuple' object has no attribute 'choice'
AttributeError: 'tuple' object has no attribute 'choice'
我是 运行 一些代码,这个错误是在我击中痣 2 次后出现的,请帮助我找到解决方案
这基本上是在我击中鼹鼠之后,它在我击中它 2 次后随机出现在任何地方,这个错误让我更了解它,我发布了一个视频,这样你就可以看到发生了什么
link 是 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 错误在代码中,所以我给出了所有代码
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()
我试图查看这是什么类型的错误并看到了一些答案,但我不知道该怎么做,请帮助我找出为什么会出现此错误
问题是您使用名称 random
作为变量,random
是模块。一旦你有了一个同名变量,你就不能再使用这个模块,因为这个变量会遮蔽这个模块。更改变量的名称:
location = random.choice(random_locations)
print(location[0])
print(location[1])
mole_spawn_new(location[0], location[1])
我是 运行 一些代码,这个错误是在我击中痣 2 次后出现的,请帮助我找到解决方案
这基本上是在我击中鼹鼠之后,它在我击中它 2 次后随机出现在任何地方,这个错误让我更了解它,我发布了一个视频,这样你就可以看到发生了什么 link 是 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 错误在代码中,所以我给出了所有代码
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()
我试图查看这是什么类型的错误并看到了一些答案,但我不知道该怎么做,请帮助我找出为什么会出现此错误
问题是您使用名称 random
作为变量,random
是模块。一旦你有了一个同名变量,你就不能再使用这个模块,因为这个变量会遮蔽这个模块。更改变量的名称:
location = random.choice(random_locations)
print(location[0])
print(location[1])
mole_spawn_new(location[0], location[1])