Rock Paper Scissors程序不起作用(Python)
作者:互联网
问题:
程序似乎不接受输入的整数.不会添加赢/输/抽奖计数,也不会在调试模式下显示计算机选择
该计划的基础设计:
编写一个程序,让用户可以在计算机上玩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)
希望这有道理吗?
标签:python,return-value,random 来源: https://codeday.me/bug/20191006/1863407.html