Rock Paper Scissors程序不起作用(Python)

时间:2014-11-03 18:28:16

标签: python random return-value

问题: 程序似乎不接受输入的整数。不会添加赢/输/抽奖计数,也不会在调试模式下显示计算机选择

该计划的基础设计: 编写一个程序,让用户可以在计算机上玩Rock,Paper,Scissors游戏。 该计划应如下工作。 显示一个菜单:

得分:0胜0平0负 (D)ebug显示计算机的选择 (新游戏 (Q)UIT

如果用户输入“Q”或“q”,程序将结束。对于新游戏,“N”或“n”,对于调试模式,“D”或“d”,其他任何内容都会导致显示错误消息。

  1. 当游戏开始时,生成1到3范围内的随机数。如果数字是1,那么计算机选择了摇滚。如果数字是2,则计算机选择了纸张。如果数字是3,那么计算机选择了剪刀。 (除非我们处于“D”ebug模式,否则不要显示计算机的选择。)
  2. 用户在键盘上输入他选择的“1-rock”,“2-paper”或“3-scissors”。
  3. 显示计算机的选择。
  4. 根据以下规则选择获胜者: •如果一个玩家选择摇滚而另一个玩家选择剪刀,则摇滚获胜。 (岩石砸碎了剪刀。) •如果一个玩家选择剪刀而另一个玩家选择纸张,那么剪刀就会赢。(剪刀剪纸。) •如果一个玩家选择纸张而另一个玩家选择摇滚,则纸张获胜。 (纸包裹着岩石。) •如果两个玩家做出相同的选择,那么游戏就是平局。
  5. 您的计划将保持赢,输和抽奖的总数。
  6. 重新显示菜单并重复游戏循环。
  7. 我的计划:

    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 :(得分: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()函数将输入作为字符串。