石头剪子布程序无法工作(Python)
问题: 程序似乎无法接受输入的整数。无法增加胜利/平局/失败的计数,也不在调试模式下显示电脑的选择。
程序的基本设计: 编写一个程序,让用户可以和电脑玩石头、剪刀、布的游戏。 程序应该这样运行: 会显示一个菜单:
得分:0 胜利,0 平局,0 失败 (D)ebug 显示电脑的选择 (N)ew game 新游戏 (Q)uit 退出
如果用户输入“Q”或“q”,程序将结束。如果输入“N”或“n”,则开始新游戏;输入“D”或“d”进入调试模式,其他输入会显示错误信息。
- 当游戏开始时,程序会生成一个1到3之间的随机数字。如果数字是1,电脑选择的是石头;如果是2,电脑选择的是布;如果是3,电脑选择的是剪刀。(除非在“D”ebug模式下,否则不显示电脑的选择。)
- 用户在键盘上输入自己的选择:“1-石头”、“2-布”或“3-剪刀”。
- 显示电脑的选择。
- 根据以下规则选出胜者: • 如果一个玩家选择石头,另一个选择剪刀,那么石头赢。(石头砸碎剪刀。) • 如果一个玩家选择剪刀,另一个选择布,那么剪刀赢。(剪刀剪断布。) • 如果一个玩家选择布,另一个选择石头,那么布赢。(布包裹石头。) • 如果两个玩家选择相同,则游戏平局。
- 你的程序会持续记录胜利、失败和平局的总数。
- 重新显示菜单,重复游戏循环。
我的程序:
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 个回答
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”。不过,如果你想输入一个字符串,就必须加上引号。下面是一个例子:
>>> 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()函数会把输入当作字符串处理。
5
我不是Python高手,但我猜想,输入的内容会是字符串,所以在和电脑的整数比较之前,你需要把它转换成整数。
我觉得你在代码上有个小窍门没用到,可以让代码更简洁。你应该能写一个单独的playgame
方法,增加一个布尔参数debugmode
,这样就可以通过这个参数来决定是否直接打印,而不是每次都调用打印。
def debugPrint(debugString, debugMode)
if debugMode
print(debugString)
希望这样说你能明白?