我想编码一个石头剪刀布游戏与评分系统,允许用户希望再次发挥和分数增加,而不是重新启动到0。你知道吗
我的代码在这里:https://pastebin.com/eRqEuwtY(也附在这封邮件上)
谢谢你的帮助。你知道吗
import random
score1 = int(0)
score2 = int(0)
def main():
while True:
player = input('What do you choose to play (R, P, S)? ')
computer = random.choice(["R", "P", "S"])
print("You chose", (player), "and the computer chose", (computer))
if computer == player:
print("It's a tie")
print("Score: You =", score1, "Computer =", score2)
if computer == "R" and player == "S":
print("Computer wins")
print("Score: You =", score1, "Computer =", score2 + 1)
if computer == "P" and player == "R":
print("Computer wins")
print("Score: You =", score1, "Computer =", score2 + 1)
if computer == "S" and player == "P":
print("Computer wins")
print("Score: You =", score1, "Computer =", score2 + 1)
if computer == "S" and player == "R":
print("You won")
print("Score: You =", score1 + 1, "Computer =", score2)
if computer == "R" and player == "P":
print("You won")
print("Score: You =", score1 + 1, "Computer =", score2)
if computer == "P" and player == "S":
print("You won")
print("Score: You =", score1 + 1, "Computer =", score2)
play_again = input("Would you like to play again? Y/N ")
if play_again == "Y":
main()
else:
print("You scored", score1, "and the computer scored", score2)
exit()
main()
正如Jasper指出的,您需要将新的分数保存到相应的变量中。下面是您的代码的重做,包括一些改进:
你的问题是,如果你赢了,你就打印分数+1。这不会将新分数保存到变量中!你知道吗
示例:
代码中的另一个问题是,每次用户想要再次播放时,都要再次调用main函数。这是无止境的递归,如果达到递归限制,将导致错误。最好这样做:
相关问题 更多 >
编程相关推荐