代码之家  ›  专栏  ›  技术社区  ›  helpthisbropls

AttributeError:“tuple”对象没有属性“choice”

  •  1
  • helpthisbropls  · 技术社区  · 2 年前

    我正在运行一些代码,这个错误是在我击中鼹鼠两次后出现的。请帮我找到解决方案

    这基本上是在我击中鼹鼠后,它会在任何地方随机移动,在我击中它两次后,这个错误变得更容易理解,我发布了一个视频,这样你就可以看到发生了什么 链接是 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'
    

    代码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()
    

    我试着看看这是什么类型的错误,并看到了一些答案,但idk怎么做请帮我找出为什么会出现这个错误

    1 回复  |  直到 2 年前
        1
  •  1
  •   Rabbid76    2 年前

    问题是你使用的是这个名字 random 对于一个变量, 随机的 这是一个模块。一旦有了同名的变量,就不能再使用该模块,因为该变量将隐藏该模块。更改变量的名称:

    location = random.choice(random_locations)
    print(location[0])
    print(location[1])
    mole_spawn_new(location[0], location[1])