Rock Paper Scissors程序不起作用(Python)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Rock Paper Scissors程序不起作用(Python),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3121字,纯文字阅读大概需要5分钟。
内容图文
![Rock Paper Scissors程序不起作用(Python)](/upload/InfoBanner/zyjiaocheng/699/244441ab0fb3406b9ec7784f4e482ab6.jpg)
问题:
程序似乎不接受输入的整数.不会添加赢/输/抽奖计数,也不会在调试模式下显示计算机选择
该计划的基础设计:
编写一个程序,让用户可以在计算机上玩Rock,Paper,Scissors游戏.
该计划应如下工作.
显示一个菜单:
得分:0胜0平0负
(D)ebug显示计算机的选择
(新游戏
(放弃
如果用户输入“Q”或“q”,程序将结束.对于新游戏,“N”或“n”,对于调试模式,“D”或“d”,其他任何东西都将导致显示错误消息.
>当游戏开始时,会生成1到3范围内的随机数.如果数字是1,那么计算机选择了摇滚.如果数字是2,则计算机选择了纸张.如果数字是3,那么计算机选择了剪刀. (除非我们处于“D”ebug模式,否则不要显示计算机的选择.)
>用户在键盘上输入他选择的“1-rock”,“2-paper”或“3-scissors”.
>显示计算机的选择.
>根据以下规则选择获胜者:
?如果一个玩家选择摇滚而另一个玩家选择剪刀,则摇滚获胜.
(岩石砸碎了剪刀.)
?如果一个玩家选择剪刀而另一个玩家选择纸张,那么剪刀就会赢.(剪刀剪纸.)
?如果一个玩家选择纸张而另一个玩家选择摇滚,则纸张获胜.
(纸包裹着岩石.)
?如果两个玩家做出相同的选择,那么游戏就是平局.
>您的计划将保持赢,输和抽奖的总数.
>重新显示菜单并重复游戏循环.
我的计划:
import random
def main():
continuing = "y"
win = 0
lose = 0
draw = 0
while continuing == "y":
print("Score:", win,"wins,", draw, "draws,", lose,"losses")
print("(D)ebug to show computer's choice")
print("(N)ew game")
print("(Q)uit")
choice = input(" ")
if choice == "n" or choice == "N":
win, draw, lose = playgame(win, draw, lose)
elif choice == "d" or choice == "D":
win, draw, lose = playgame2(win, draw, lose)
elif choice == "q" or choice == "Q":
break
def playgame(win, draw, lose):
computer = random.randint(1,3)
player = input("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ")
if computer == 1 and player == 2:
Score = "You won"
win += 1
elif computer == 1 and player == 3:
Score = "You lost"
lose += 1
elif computer == 2 and player == 1:
Score = "You lost"
lose += 1
elif computer == 2 and player == 3:
Score = "You won"
win += 1
elif computer == 3 and player == 1:
Score = "You won"
win += 1
elif computer == 3 and player == 2:
Score = "You lost"
lose += 1
elif computer == player:
Score = "Draw"
draw += 1
return (win, draw, lose)
def playgame2(win, draw, lose):
computer = random.randint(1, 3)
player = input("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ")
if computer == 1 and player == 2:
Score = "You won"
print("Computer chose rock")
win += 1
elif computer == 1 and player == 3:
Score = "You lost"
print("Computer chose rock")
lose += 1
elif computer == 2 and player == 1:
Score = "You lost"
print("Computer chose paper")
lose += 1
elif computer == 2 and player == 3:
Score = "You won"
print("Computer chose paper")
win += 1
elif computer == 3 and player == 1:
Score = "You won"
print("Computer chose scissors")
win += 1
elif computer == 3 and player == 2:
Score = "You lost"
print("Computer chose scissors")
lose += 1
elif computer == player:
Score = "Draw"
print("Computer chose the same as you")
draw += 1
return (win, draw, lose)
main()
解决方法:
我不是Pythonista,但是在猜测,input returns strings,你需要在与计算机的int比较之前转换为整数.
我还认为你在DRYing up你的代码中缺少一个技巧 – 你应该能够有一个playgame方法,它需要一个额外的布尔参数debugmode,而不是直接调用print,调用一个间接,例如:
def debugPrint(debugString, debugMode)
if debugMode
print(debugString)
希望这有道理吗?
内容总结
以上是互联网集市为您收集整理的Rock Paper Scissors程序不起作用(Python)全部内容,希望文章能够帮你解决Rock Paper Scissors程序不起作用(Python)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。