所以我遇到的问题是,当我的计数器试图将数字相加时,它会不断重置 我试着想办法把计数器部分变成一个函数,但我想不出来
winP1=0
winP2=0
tie=0
ans = input('Would you like to play ROCK, PAPER, SCISSORS?: ')
while ans == 'y':
p1c = input('Player 1 enter either R,P, or S: ') #p1c stands for "player 1 choice"
p2c = input('Player 2 enter either R,P, or S: ')
ans = input('Would you like to play again: ')
def game(p1c,p2c):
if p1c == p2c: #if its a tie we are going to add to the Tie variable
return 0
elif p1c == 'R' and p2c == 'P': #We will only set the Player 1 wins because
return 1 #player 2 wins can be set as else
elif p1c == 'P' and p2c == 'S':
return 1
elif p1c == 'S' and p2c == 'R':
return 1
else:
return -1
result = game(p1c,p2c)
if result == -1:
winP2 += 1
if result == 0:
tie += 1
else:
winP1 +=1
print('Player 1 won {} times. \nPlayer 2 won {} times. \nThere were {} ties.'.format(winP1,winP2,tie))
欢迎使用StackOverflow和Python编程!我希望你在这里过得愉快。你知道吗
你的计数器不断重置,因为对系统来说,你只玩了一个游戏:
我将在你的代码中移动一些东西,以得到你想要的结果,进行最小限度的编辑,希望这能告诉你你需要做什么。你在正确的轨道上!你知道吗
我们将使用一个名为
state
的字典来跟踪游戏状态并传递它。你知道吗相关问题 更多 >
编程相关推荐