【发布时间】:2015-06-17 15:46:36
【问题描述】:
我是一个刚起步的基本程序员,我有这个任务,在游戏结束时,它会询问用户是否想再玩一次,然后他们输入 Y 或 N。我的问题是什么时候他们打 Y 再玩一次,我的 def game() 打印出上一场比赛的第一场比赛的结果,而不是重复整个比赛。我的其余代码运行良好。 到目前为止,这是我的代码:
`def intro():
print "Welcome to Rock, Paper, Scissors."
print "This is a game between two opponents!"
print "Have Fun!"
print
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
print "Player 1 wins"
return 1
elif player1 == "s" and player2 == "p":
print
print "Player 1 wins"
return 1
elif player1 == "r" and player2 == "s":
print
print "Player 1 wins"
return 1
elif player1 == "p" and player2 == "s":
print
print "Player 2 wins"
return 2
elif player1 == "s" and player2 == "r":
print
print "Player 2 wins"
return 2
elif player1 == "r" and player2 == "p":
print
print "Player 2 wins"
return 2
elif player1 == "p" and player2 == "p":
print
print "Tie"
return 0
elif player1 == "s" and player2 == "s":
print
print "Tie"
return 0
elif player1 == "r" and player2 == "r":
print
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
print "Player 1's score is ", score1
print "Player 2's score is ", score2
elif game1 == 2:
score2 = score2 + 1
print
print "Player 1's score is ", score1
print "Player 2's score is ", score2
elif game1 == 0:
print
print "Player 1's score is ", score1
print "Player 2's score is ", score2
print
print "Get ready for Round 2!"
print
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
print "Player 1's score is ", score1
print "Player 2's score is ", score2
elif game2 == 2:
score2 = score2 + 1
print
print "Player 1's score is ", score1
print "Player 2's score is ", score2
elif game2 == 0:
print
print "Player 1's score is ", score1
print "Player 2's score is ", score2
print
print "Final Round!"
print
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
print "Player 1's final score is ", score1
print "Player 2's final score is ", score2
elif game2 == 2:
score2 = score2 + 1
print
print "Player 1's final score is ", score1
print "Player 2's final score is ", score2
elif game2 == 0:
print
print "Player 1's final score is ", score1
print "Player 2's final score is ", score2
rematch()`
现在是这里:
`def rematch():
while 1:
retry = raw_input("Would you like to play again? Y or N: ")
if retry == 'Y':
game()
else:
ending()
break`
在我看来,game() 将开始一个全新的游戏,但就像我提到的那样,它只是打印上一个游戏的结果。
任何帮助都会很棒,因为这是我第一次编码,也是第一次在这个网站上。谢谢。
【问题讨论】:
标签: python python-2.7 loops while-loop