Python 石头、布、剪刀和重启选项
我刚刚开始使用python,需要一些帮助!我正在做一个石头、布、剪刀的游戏,我想在人类或计算机达到3胜时添加一个重启选项 我已经到处寻找一些答案,但从我所看到的所有其他代码来看,似乎与我所写的完全不同。我没有尝试过使用def和类,我看到了很多,并且使它看起来非常简单。我知道我可能会以一种非常迂回的方式来做这件事,但我只想在不完全复制别人代码的情况下完成这件事Python 石头、布、剪刀和重启选项,python,Python,我刚刚开始使用python,需要一些帮助!我正在做一个石头、布、剪刀的游戏,我想在人类或计算机达到3胜时添加一个重启选项 我已经到处寻找一些答案,但从我所看到的所有其他代码来看,似乎与我所写的完全不同。我没有尝试过使用def和类,我看到了很多,并且使它看起来非常简单。我知道我可能会以一种非常迂回的方式来做这件事,但我只想在不完全复制别人代码的情况下完成这件事 import random moves = ["Rock", "Paper", "Scissors"] play
import random
moves = ["Rock", "Paper", "Scissors"]
player_score = 0
computer_score = 0
draws = 0
keep_playing = True
while keep_playing == True:
cmove = random.choice(moves)
pmove = input("What is your move: Rock, Paper, or Scissors?")
print("The computer chose", cmove)
#Logic to game
if cmove == pmove:
print("It's a DRAW!")
elif pmove == "Rock" and cmove == "Scissors":
print("--Player Wins!--")
elif pmove == "Rock" and cmove == "Paper":
print("--Computer Wins!--")
elif cmove == "Paper" and cmove == "Rock":
print("--Player Wins!--")
elif pmove == "Paper" and cmove == "Scissors":
print("--Computer Wins!--")
elif pmove == "Scissors" and cmove == "Paper":
print("--Player Wins!--")
elif pmove == "Scissors" and cmove == "Rock":
print("--Computer Wins!--")
#Scoreboard
if pmove == cmove:
draws = draws + 1
print("Player:" + str(player_score) + ' | ' + "Computer:" + str(computer_score) + ' | ' + "Draws:" + str(draws))
if pmove == "Rock" and cmove == "Scissors":
player_score = player_score + 1
print("Player:" + str(player_score) + ' | ' + "Computer:" + str(computer_score) + ' | ' + "Draws:" + str(draws))
if pmove == "Rock" and cmove == "Paper":
computer_score = computer_score + 1
print("Player:" + str(player_score) + ' | ' + "Computer:" + str(computer_score) + ' | ' + "Draws:" + str(draws))
if pmove == "Paper" and cmove == "Rock":
player_score = player_score + 1
print("Player:" + str(player_score) + ' | ' + "Computer:" + str(computer_score) + ' | ' + "Draws:" + str(draws))
if pmove == "Paper" and cmove == "Scissors":
computer_score = computer_score + 1
print("Player:" + str(player_score) + ' | ' + "Computer:" + str(computer_score) + ' | ' + "Draws:" + str(draws))
if pmove == "Scissors" and cmove == "Paper":
player_score = player_score + 1
print("Player:" + str(player_score) + ' | ' + "Computer:" + str(computer_score) + ' | ' + "Draws:" + str(draws))
if pmove == "Scissors" and cmove == "Rock":
computer_score = computer_score + 1
print("Player:" + str(player_score) + ' | ' + "Computer:" + str(computer_score) + ' | ' + "Draws:" + str(draws))
#Win/lose restart point?
if player_score == 3:
print("-----You Win!-----")
break
if computer_score == 3:
print("-----You Lose!-----")
break
我希望代码结束时说“你赢了!”或“你输了!”,然后要求输入他们是否想重新开始,然后它重置分数并继续运行,或者如果他们说不,它就会中断。你需要在
循环时获得用户输入,并相应地设置继续播放
while keep_playing == True:
cmove = random.choice(moves)
pmove = input("What is your move: Rock, Paper, or Scissors?")
print("The computer chose", cmove)
#Logic to game
if cmove == pmove:
print("It's a DRAW!")
elif pmove == "Rock" and cmove == "Scissors":
print("--Player Wins!--")
elif pmove == "Rock" and cmove == "Paper":
print("--Computer Wins!--")
elif cmove == "Paper" and cmove == "Rock":
print("--Player Wins!--")
elif pmove == "Paper" and cmove == "Scissors":
print("--Computer Wins!--")
elif pmove == "Scissors" and cmove == "Paper":
print("--Player Wins!--")
elif pmove == "Scissors" and cmove == "Rock":
print("--Computer Wins!--")
# new code
play_again = input("Would you like to play again?")
keep_playing = play_again.lower().startswith('y') # this will be set to False if the user's input doesn't start with 'y', the loop will exit
您已经有了执行此操作的循环。所以当你想“重启”你的游戏时,你真的只需要重置分数
从您的赢/输条件开始:
#Win/lose restart point?
if player_score == 3:
print("-----You Win!-----")
replay = input("Would you like to play again?")
if replay.upper().startswith('Y'):
player_score = 0
computer_score = 0
draws = 0
else:
keep_playing = False
if computer_score == 3:
print("-----You Lose!-----")
if replay.upper().startswith('Y'):
player_score = 0
computer_score = 0
draws = 0
else:
keep_playing = False
完美的这正是我想要做的!非常感谢你!