python石头剪子布while循环_初学者使用while循环重复石头、布、剪刀游戏?

我是一个基本的程序员,刚刚开始,我有一个任务,在游戏结束时,它会询问用户是否想再次玩游戏,然后他们输入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()`

就在这里:

^{pr2}$

在我看来,我认为game()将开始一个新的游戏,但是就像我提到的,它只是打印上一个游戏的结果。在

这也是我第一次在网站上写代码的第一次。谢谢。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值