我正在制作的程序是一个骰子游戏,允许两个用户各掷2个6面骰子。但是我不确定如何正确插入它,因此分数变量会因为不起作用而成为全局变量。玩家1的得分位于变量p1score下,而玩家2的得分存储在变量p2score下。
def roundtwo():
p1r2 = input("Player 1 are you ready for round 2 type y = yes n= no: ")
if p1r2 == 'y' or p1r2 == 'Y':
p1dice1 = random.randint(min,max)
p1dice2 = random.randint(min,max)
print("The value of dice 1 is:", p1dice1)
print("The value of dice 2 is:", p1dice2)
if (p1dice1 % 2) == 0 :
p1score = p1score + p1dice1 + p1dice2
p1score = p1score + 10
print("The results for Player 1 in round 2 is: ", p1score)
elif (p1dice2 % 2) == 0:
p1score = p1score + p1dice1 + p1dice2
p1score = p1score + 10
print("The results for Player 1 in round 2 is: ", p1score)
elif (p1dice1 % 2) == 0 and (p1dice2 % 2) == 0:
p1score = p1score + p1dice1 + p1dice2
p1score = p1score + 20
print("The results for Player 1 in round 2 is: ", p1score)
else:
p1score = p1score + p1dice1 + p1dice2
print("The results of Player 1 in round 2 is: ", p1score)
else:
user()
我需要它来为两个玩家计算第二回合的积分。因此,如果在第一回合中玩家1获得3,然后在第二回合中获得14,则第一回合中玩家1的得分应为17。
答案 0 :(得分:0)
在此行中:p1score = p1score + p1dice1 + p1dice2
是否已将p1score
设置为0?如果没有,那么此行可以为您提供分配错误之前引用的局部变量