Problems: Program does not seem to accept the integers entered. Won't add to win/loss/draw count and does not display computer choice in debug mode
Basics Design of the Program: 编写一个程序,让用户可以在计算机上玩Rock,Paper,Scissors游戏 . 该计划应如下工作 . 显示一个菜单:
得分:0胜0平0负(D)ebug显示计算机的选择(N)ew游戏(Q)uit
如果用户输入“Q”或“q”,程序将结束 . 对于新游戏,“N”或“n”,对于调试模式,“D”或“d”,其他任何东西都将导致显示错误消息 .
-
游戏开始时,会生成1到3范围内的随机数 . 如果数字是1,那么计算机选择了摇滚 . 如果数字是2,则计算机选择了纸张 . 如果数字是3,那么计算机选择了剪刀 . (Don 't display the computer'的选择,除非我们处于"D" ebug模式 . )
-
用户在键盘上输入他选择的“1-rock”,“2-paper”或“3-scissors” .
-
显示计算机的选择 .
-
根据以下规则选择获胜者:•如果一个玩家选择摇滚而另一个玩家选择剪刀,则摇滚获胜 . (岩石砸碎剪刀 . )•如果一个玩家选择剪刀而另一个玩家选择纸张,那么剪刀就会胜利 . (剪刀剪纸 . )•如果一个玩家选择纸张而另一个玩家选择摇滚,那么纸张就会赢 . (纸包裹摇滚 . )•如果两个玩家做出相同的选择,那么游戏就是平局 .
-
您的计划将保持赢,输和抽奖的总数 .
-
重新显示菜单并重复游戏循环 .
My Program:
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()
2 回答
我不是Pythonista,但是猜测,input returns strings,你'll need to convert to integer before comparing to the computer' s int .
我也认为你错过了DRYing up你的代码 - 你应该能够有一个
playgame
方法,它需要一个额外的布尔参数debugmode
,而不是直接调用print,调用一个间接,例如:希望这有道理吗?
这适用于Python 2.x,但不适用于Python 3.x在Python 3.x中,input()返回字符串 . 因此,玩家的输入将是“1”或“2”或“3”的形式 . 由于1和“1”不同,程序不会执行playgame()和playgame2()中if和elif块中的任何行 . 这是一个Python 3.x示例:
因此,您应该在任何需要整数输入的地方使用i = int(input(“Input =”)) .
但是,在Python 2.x中,input()将1作为1本身而不是“1” . 但是,当您想要将字符串键入为inpu时,您还必须提供引号 . 这是一个例子:
在Python 2.x中,raw_input()函数将输入作为字符串 .