【发布时间】:2021-06-29 16:05:24
【问题描述】:
我正在尝试使用循环为我的棋盘游戏创建骰子。但是,变量 pos 不会自我更新,因此下次触发 for 循环时,它会从其先前的值而不是 0 开始。在每次触发 for 循环时,pos 默认从 0 开始并且不会每次运行后都不会更新。这意味着玩家总是会回到 平方 0。
这是我迄今为止编写的代码:
w = True
while w == True:
dice1 = random.randint(1, 6)
player1_dice = input("\nBULL (P1): Please press 'ENTER' to roll the dice ")
if player1_dice == "":
print("BULL (P1) rolled a", dice1)
elif player1_dice != "":
print("BULL (P1): Please make sure to press 'ENTER' ")
dieValue = dice1
pos = 0
for pos in range(dieValue) :
posi = pos + 1
P1_pos = player1.goto(p1_list[posi])
pos = posi
【问题讨论】:
-
为什么你认为
pos = 0行不会将变量重置为0? -
您不能让
pos既是保留值又是循环变量。将pos = 0移出循环,并将循环变量更改为i。然后循环变为pos += 1/P1_pos = platyr1.goto(p1_list[pos]) -
谢谢,现在可以使用了。我不知道为什么我一开始就没有想到。
标签: python for-loop variables while-loop dice