python - 石头、纸、剪刀和重启选项
问题描述
我刚开始使用 python 并且可以使用一些帮助!我正在开发一个石头、纸、剪刀游戏,一旦人类或计算机达到 3 胜,我想添加一个重新启动选项。
我已经到处寻找一些答案,但从我查看的所有其他代码看来,这似乎超出了我的范围,或者与我编写的代码完全不同。我没有尝试过使用 def 和 classes,我看到了很多,并且让它看起来很简单。我知道我可能会以一种非常全面的方式来解决这个问题,但我只想在不完全复制某人代码的情况下完成它。
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
推荐阅读
- polymer-3.x - 如何缩短 Polymer 3.0 的构建时间
- wpf - 使用 MEF 和 ExportAttribute 加载视图时出现问题
- angular - 为什么在单元测试ngrx-effect时出现此错误
- javascript - 检查多个输入是否为数字类型
- mysql - 我无法通过 Mysql 查询在 Wordpress 管理面板中更改库存状态和管理库存选项
- javascript - 基于布尔值启用/禁用 Vue 转换
- javascript - 如何以角度重新加载 index.html 的 head 标签内的脚本?
- http - Go http 客户端不会自动拆分正文
- ssl - Cert-Manager 证书更新过程 - 它是如何执行的?
- javascript - 使用 JavaScript 返回 3 维数组中最大值的索引