我刚刚开始使用 python,需要一些帮助!我当时在做剪刀石头布游戏,我想在人或计算机赢得 3 场胜利后添加一个重启选项。
我已经四处寻找一些答案,但从我看到的所有其他代码来看,似乎超出了我的范围,或者与我编写的内容截然不同。我没有尝试使用 def 和类,我看到了很多,并使它看起来非常简单。我知道我可能会以一种非常迂回的方式来解决这个问题,但我只想在不完全复制某人的代码的情况下完成它。
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
我希望代码结束时说“你赢了!”或“你输了!”,然后询问他们是否要重新开始,然后它会重置分数并继续进行,或者如果他们拒绝,它就会中断。
请您参考如下方法:
您已经有了执行此操作的循环。因此,当您想“重新开始”游戏时,您实际上只需要重新设置分数即可。
从你的赢/输条件开始:
#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
