【发布时间】:2021-04-04 13:26:18
【问题描述】:
:D 我用 Python 制作了一个小石头剪刀布游戏,只是为了好玩和练习,我一直在尝试实现一个小评分系统,它似乎不想正常工作,我不确定如何解决问题。
import random
import messages
def gameOn():
choice = ["rock", "paper", "scissors"]
computer_choice = random.choice(choice)
player_choice = input("Please chose Rock/Paper/Scissors: ")
player_choice = player_choice.lower()
total_score = 0
while True:
#Makes sure the user enters a valid option.
if player_choice not in("rock", "paper", "scissors"):
print("Choice is not correct!")
#Prints in case both the computer and the player chose the same option.
elif computer_choice == player_choice:
print("You chose the same.")
#Computer choses ROCK.
elif computer_choice == "rock" and player_choice == "paper":
print(messages.win[0])
total_score += 1
print(total_score)
elif computer_choice == "rock" and player_choice == "scissors":
print(messages.lose[0])
#Computer choses PAPER.
elif computer_choice == "paper" and player_choice == "rock":
print(messages.lose[1])
elif computer_choice == "paper" and player_choice == "scissors":
print(messages.win[1])
total_score += 1
print(total_score)
#Computer choses SCISSORS.
elif computer_choice == "scissors" and player_choice == "rock":
print(messages.win[2])
total_score += 1
print(total_score)
elif computer_choice == "scissors" and player_choice == "paper":
print(messages.lose[2])
#Asks the user if he/she wants to play again and restarts the loop if so.
answer = input("Would you like to play again or see your score? Yes/No/Score ")
if answer in ("yes", "Yes", "y", "Y", "yup"):
print("Game starting again!")
gameOn()
elif answer in ("Score", "score"):
print("Your total score is " + str(total_score))
answer = input("Would you like to play again or see your score? Yes/No/Score ")
print("Game starting again!")
gameOn()
else:
print("Goodbye!")
break
gameOn()
增量本身有效,但我想做的是,如果玩家赢得多轮比赛并在最后写下“分数”,他应该能够看到他获得的所有分数。目前,每次新游戏开始时,得分变量都会重置,因此如果他赢得了回合,则使用的得分始终为 0 或 1。我怎样才能防止这种行为? 非常感谢:D,我希望这不是一个太愚蠢的问题。
【问题讨论】:
-
请记住,存在递归限制,因此您只能在程序崩溃之前玩 1000 次左右的游戏,因为有很多嵌套函数调用。
-
我对此一无所知 :D 谢谢你告诉我。
标签: python python-3.x increment