我是一个基本的程序员,刚刚开始,我有一个任务,在游戏结束时,它会询问用户是否想再次玩游戏,然后他们输入Y或N。我的问题是当他们点击Y再次玩时,我的def game()会打印出上一场比赛的第一场比赛的结果,而不是重复整个游戏。我的其余代码运行良好。
以下是我目前为止的代码:`def intro():
print "Welcome to Rock, Paper, Scissors."
print "This is a game between two opponents!"
print "Have Fun!"
def rematch():
while 1:
retry = raw_input("Would you like to play again? Y or N: ")
if retry == 'Y':
game()
else:
ending()
break
def ending():
print "Thank you for playing!"
print "Please come back and try again."
def game():
if player1 == "p" and player2 == "r":
print "Player 1 wins"
return 1
elif player1 == "s" and player2 == "p":
print "Player 1 wins"
return 1
elif player1 == "r" and player2 == "s":
print "Player 1 wins"
return 1
elif player1 == "p" and player2 == "s":
print "Player 2 wins"
return 2
elif player1 == "s" and player2 == "r":
print "Player 2 wins"
return 2
elif player1 == "r" and player2 == "p":
print "Player 2 wins"
return 2
elif player1 == "p" and player2 == "p":
print "Tie"
return 0
elif player1 == "s" and player2 == "s":
print "Tie"
return 0
elif player1 == "r" and player2 == "r":
print "Tie"
return 0
score1 = 0
score2 = 0
intro()
player1 = raw_input("Player 1 what is your choice, (R)ock, (P)aper, or (S)cissors? ").lower()
player2 = raw_input("Player 2 what is your choice, (R)ock, (P)aper, or (S)cissors? ").lower()
game1 = game()
if game1 == 1:
score1 = score1 + 1
print "Player 1's score is ", score1
print "Player 2's score is ", score2
elif game1 == 2:
score2 = score2 + 1
print "Player 1's score is ", score1
print "Player 2's score is ", score2
elif game1 == 0:
print "Player 1's score is ", score1
print "Player 2's score is ", score2
print "Get ready for Round 2!"
player3 = raw_input("Player 1 what is your choice, (R)ock, (P)aper, or (S)cissors? ").lower()
player4 = raw_input("Player 2 what is your choice, (R)ock, (P)aper, or (S)cissors? ").lower()
game2 = game()
if game2 == 1:
score1 = score1 + 1
print "Player 1's score is ", score1
print "Player 2's score is ", score2
elif game2 == 2:
score2 = score2 + 1
print "Player 1's score is ", score1
print "Player 2's score is ", score2
elif game2 == 0:
print "Player 1's score is ", score1
print "Player 2's score is ", score2
print "Final Round!"
player5 = raw_input("Player 1 what is your choice, (R)ock, (P)aper, or (S)cissors? ").lower()
player6 = raw_input("Player 2 what is your choice, (R)ock, (P)aper, or (S)cissors? ").lower()
game3 = game()
if game2 == 1:
score1 = score1 + 1
print "Player 1's final score is ", score1
print "Player 2's final score is ", score2
elif game2 == 2:
score2 = score2 + 1
print "Player 1's final score is ", score1
print "Player 2's final score is ", score2
elif game2 == 0:
print "Player 1's final score is ", score1
print "Player 2's final score is ", score2
rematch()`
就在这里:
^{pr2}$
在我看来,我认为game()将开始一个新的游戏,但是就像我提到的,它只是打印上一个游戏的结果。在
这也是我第一次在网站上写代码的第一次。谢谢。在