Rock Paper Scissors Program Not Working (Python)

前端 未结 2 1472
感情败类
感情败类 2021-01-07 01:07

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

相关标签:
2条回答
  • 2021-01-07 01:55

    This would work in Python 2.x, but, not in Python 3.x In Python 3.x, input() returns strings. Thus, the player's input would be of the form "1" or "2" or "3". Since 1 and "1" are different, the program will not execute any of the lines in the if and elif blocks in playgame() and playgame2(). Here is a Python 3.x example:

    >>> a = input("Input = ")
    Input = 1
    >>> print a
    SyntaxError: Missing parentheses in call to 'print'
    >>> print(a)
    1
    >>> a
    '1'
    >>> type(a)
    <class 'str'>
    

    Thus, you should use i = int(input("Input = ")) wherever you want an integer input.

    However, in Python 2.x, input() will take 1 as 1 itself and not as "1". But, when you want to type a string as an inpu, you will have to give the quotes also. Here is an exxample:

    >>> 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'>
    >>> 
    

    In Python 2.x, the raw_input() function takes the input as a string.

    0 讨论(0)
  • 2021-01-07 02:01

    I'm no Pythonista, but at a guess, input returns strings, and you'll need to convert to integer before comparing to the computer's int.

    I also think you are missing a trick in DRYing up your code - you should be able to have a single playgame method, which takes an additional boolean parameter debugmode, which instead of calling print directly, calls an indirection, e.g.:

    def debugPrint(debugString, debugMode)
         if debugMode
             print(debugString)
    

    Hope this makes sense?

    0 讨论(0)
提交回复
热议问题