问题: 程序似乎不接受输入的整数。不会添加赢/输/抽奖计数,也不会在调试模式下显示计算机选择
该计划的基础设计: 编写一个程序,让用户可以在计算机上玩Rock,Paper,Scissors游戏。 该计划应如下工作。 显示一个菜单:
得分:0胜0平0负 (D)ebug显示计算机的选择 (新游戏 (Q)UIT
如果用户输入“Q”或“q”,程序将结束。对于新游戏,“N”或“n”,对于调试模式,“D”或“d”,其他任何内容都会导致显示错误消息。
我的计划:
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()
答案 0 :(得分:4)
我不是Pythonista,但是在猜测input returns strings,你需要在与计算机的int进行比较之前转换为整数。
我还认为你错过了DRYing up你的代码中的一个技巧 - 你应该能够有一个playgame
方法,它需要一个额外的布尔参数debugmode
,而不是直接调用print,调用间接,例如:
def debugPrint(debugString, debugMode)
if debugMode
print(debugString)
希望这有道理吗?
答案 1 :(得分:0)
这将在Python 2.x中工作,但在Python 3.x中不行 在Python 3.x中,input()返回字符串。因此,玩家的输入将具有“ 1”或“ 2”或“ 3”的形式。由于1和“ 1”不同,因此该程序将不会执行playgame()和playgame2()中if和elif块中的任何行。 这是一个Python 3.x示例:
>>> a = input("Input = ")
Input = 1
>>> print a
SyntaxError: Missing parentheses in call to 'print'
>>> print(a)
1
>>> a
'1'
>>> type(a)
<class 'str'>
因此,无论何时要输入整数,都应使用i = int(input(“ Input =”)))。
但是,在Python 2.x中,input()本身将1设为1,而不是1。但是,当您想将字符串输入为inpu时,还必须加上引号。这是一个例子:
>>> a1 = input("Input = ")
Input = 1
>>> a1
1
>>> type(a1)
<type 'int'>
>>> #I want to input a string now:
>>> a2 = input("Input = ")
Input = string
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
a2 = input("Input = ")
File "<string>", line 1, in <module>
NameError: name 'string' is not defined
>>> a2 = input("Input = ")
Input = "string"
>>> a2
'string'
>>> type(a2)
<type 'str'>
>>> a3 = raw_input("Input = ")
Input = hello
>>> a3
'hello'
>>> type(a3)
<type 'str'>
>>>
在Python 2.x中,raw_input()函数将输入作为字符串。