我正在尝试创建棋盘游戏对不起!但我似乎找不到错误。它给了我一个错误:
“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()
字符串
2条答案
按热度按时间muk1a3rh1#
在调试时,特别是在Stack Overflow上发布时,最好编写一个最小的问题示例,并使用少量的实验代码。这与你的原始版本有相同的问题,更容易发现问题:
字符串
产出
型
这样更好用问题是
gamble_roll
返回骰子的总和,但您希望得到两个骰子的值。更改gamble_roll
的返回值,它将工作。型
顺便说一句,
random.choice
和randint
代码做的事情是一样的,但更简洁。你并不需要那些局部变量。型
bpsygsoo2#
您可能希望将此
return roll_uno + roll_dos
替换为此return roll_uno, roll_dos