我有下面的代码,但是当playing=false时,它不能控制流以允许player2掷骰子。有人能发现错误吗?基本上,它从来没有得到:滚轴骰子2和我不知道为什么
注意:我在RollTwoDiceP1中尝试将playing(布尔变量)设置为false,希望在返回playerturns()函数时,这次将转到RollTwoDiceP2(Player2 turn sub)。那不行
def callmatrix(player1,player2, n):
print("*************LOADING GAME******************")
print("Welcome:", player1,"and", player2)
for i in matrix(n):
print(i)
playing = True
playerturns(player1,player2,playing)
def playerturns(player1,player2,playing):
print(" - - - - - - - - - - ")
print("Press Enter to contnue")
#playing = True
while(playing):
roll=input()
if roll=="r" or "R":
RollTwoDiceP1(player1,player2)
else:
RollTwoDiceP2(player1,player2)
def RollTwoDiceP1(player1,player2):
turn=input("Player 1, it's your turn to roll the dice: Press r to roll:>>>")
#create two variables here and assign them random numbers
die1=random.randint(1,6)
die2=random.randint(1,6)
#add the two die numbers together
roll=die1+die2
#when you are printing an integer, you need to cast it into a string before you printit
print("Player1: You rolled a:", die1, "and a", die2, "which give you a:", roll)
playing = False
playerturns(player1,player2,playing)
def RollTwoDiceP2(player1,player2):
turn=input("Player 2, it's your turn to roll the dice: Press r to roll:>>>")
#create two variables here and assign them random numbers
die1=random.randint(1,6)
die2=random.randint(1,6)
#add the two die numbers together
roll=die1+die2
print("Player2: You rolled a:", die1, "and a", die2, "which give you a:", roll)
playing = True
playerturns(player1,player2,7,playing)
输出:
Continually asks Player 1 to Roll. Prints the result of Player 1s roll (repeat)
这是一个逻辑错误,因此它不是指定问题的重复
问题是行
if roll=="r" or "R":
。首先,我们计算roll=="r"
,它可能是真的,也可能是假的,然后是"R"
,它总是真的。因为它与一个or
组合在一起,所以语句总是true,else
分支不会被执行。将语句更改为if roll == "r" or roll == "R":
或更好的if roll.lower() == "r":
相关问题 更多 >
编程相关推荐