python-3.x TypeError:cannot unpack non-iterable int object for Sorry!游戏

suzh9iv8  于 2023-08-08  发布在  Python
关注(0)|答案(2)|浏览(111)

我正在尝试创建棋盘游戏对不起!但我似乎找不到错误。它给了我一个错误:
“TypeError:cannot unpack non-iterable int object”第33行。我将在给出错误的行下面划线。
我还需要帮助插入“push enter to 'gamble'”功能,因为我不知道如何插入键盘功能。

import random  #***import random module******import random module******import random module***

participants = int(input("Enter the amount of players: ")) #asks for amount of players

players = []

for i in range(participants): #asks for player's names
    names = input(f"Enter the player's names: {i+1}: ") #player inputs name
    players.append(names) #adds name to player
    
sorry_board = [0] * 50 #board total spaces

dice_uno = [ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
dice_dos = [ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

def gamble_roll():
    roll_uno = dice_uno[random.randint(0, len(dice_uno)-1)]#len used to retrieve length of list
    roll_dos = dice_dos[random.randint(0, len(dice_dos)-1)]#random function used to generate random number
    return roll_uno + roll_dos

def player_movement (player, gamble):
    sorry_board[player] += gamble
    for i in range(participants):
        if i != participants and sorry_board[i] == sorry_board[player]:
            sorry_board[i] = 0

def board_hud (): #displays board #hud=heads_up_display
    for i in range(participants):
        print(f"{players[i]}: {sorry_board[i]}")              
gambles = [0] * participants
while True:
    for i in range(participants):   
        dice_uno, dice_dos =  gamble_roll()
        if dice_uno == dice_dos:
            gambles[i] += 1
            if gambles[i] == 2:
                sorry_board[i] = 0
                gambles[i] == 0
            else:
                player_movement(i, dice_uno)
        else:
            gambles[i] = 0
            player_movement(i, dice_uno)
        if sorry_board[i] >= 50:
            print(f"{players[i]} wins!")
            
            play_again = input("Would you like to play again? ")
            if play_again.lower() == "yes":
                sorry_board = [0] * 50
                gambles = [0] * participants
            else:
                exit()
            
            board_hud()

字符串

muk1a3rh

muk1a3rh1#

在调试时,特别是在Stack Overflow上发布时,最好编写一个最小的问题示例,并使用少量的实验代码。这与你的原始版本有相同的问题,更容易发现问题:

import random

dice_uno = [ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
dice_dos = [ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

def gamble_roll():
    roll_uno = dice_uno[random.randint(0, len(dice_uno)-1)]#len used to retrieve length of list
    roll_dos = dice_dos[random.randint(0, len(dice_dos)-1)]#random function used to generate random number
    return roll_uno + roll_dos

dice_uno, dice_dos =  gamble_roll()

字符串
产出

Traceback (most recent call last):
  File "/home/td/tmp/d/j.py", line 11, in <module>
    dice_uno, dice_dos =  gamble_roll()
TypeError: cannot unpack non-iterable int object


这样更好用问题是gamble_roll返回骰子的总和,但您希望得到两个骰子的值。更改gamble_roll的返回值,它将工作。

import random

dice_uno = [ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
dice_dos = [ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

def gamble_roll():
    roll_uno = dice_uno[random.randint(0, len(dice_uno)-1)]#len used to retrieve length of list
    roll_dos = dice_dos[random.randint(0, len(dice_dos)-1)]#random function used to generate random number
    return roll_uno, roll_dos

dice_uno, dice_dos =  gamble_roll()


顺便说一句,random.choicerandint代码做的事情是一样的,但更简洁。你并不需要那些局部变量。

def gamble_roll():
    return random.choice(dice_uno), random.choice(dice_duo)

bpsygsoo

bpsygsoo2#

您可能希望将此return roll_uno + roll_dos替换为此return roll_uno, roll_dos

相关问题