不知道为什么我的代码不起作用(初学者编码器)

时间:2019-05-19 17:57:08

标签: python visual-studio-code

我使用Python创建了一个石头,纸,剪刀的游戏,用户可以输入他们的选择,然后计算机会自己随机选择一个选项。每当用户输入条目时,我的代码都会给我一个Traceback错误,该错误已包含在代码中。

注意::我的代码可以在Python IDLE中正常运行,但是我只能在VS Code中遇到此问题,并且试图找出原因。

我已经尝试配置JSON文件,并且该代码的副本也存在。

#rock, paper, scissors

from random import randint #to import random numbers



user = input('rock (r), paper (p), scissors (s)?') #Let's the user input something and assigns
    #it to whatever option it picked. So the next line, user will show as r, p, or s. 

print(user, 'against') #Just prints out the user input and the string against.

random = randint (1,3) #Sets the range of the random integer to all numbers between
    #1 and 3, which also includes 1 and 3.

if random == 1:
    computerChoice = 'rock' #Assigning the random integers to a specific string.

elif random == 2:
    computerChoice = 'paper' 

else:
    computerChoice = 'scissors'

#     """COMMENT: The reason why else doesn't have a option like else random == 3:
#         is because else is used when it has to evaluate EVERYTHING else that is left, if you want
#         to make this    user = input('rock (r), paper (p), scissors (s)?') #Let's the user input something and assigns
#         it to whatever option it picked. So the next line, user will show as r, p, or s.""" 



# """COMMENT: The reason why else doesn't have a option like else random == 3:
#         is because else is used when it has to evaluate EVERYTHING else that is left, if you want
#         to make this more restrictive, then just use another elif statement."""

print(computerChoice)

if user == computerChoice: #So it can output if something is a draw. 
    print('Draw!')

elif user == 'rock' and computerChoice == 'scissors': #The colon at the end is important because
    print('You won!')

elif user == 'rock' and computerChoice == 'paper':
    print('You lost!')

elif user == 'paper' and computerChoice == 'rock':
    print('You won!')

elif user == 'paper' and computerChoice == 'scissors':
    print('You lost!')

elif user == 'scissors' and computerChoice == 'paper':
    print('You won!')

elif user == 'scissors' and computerChoice == 'rock':
    print('You lost!')

    # """COMMENT: The code above consists of If and else statements that makes sure to include all
    # possible outcomes of this game, Since there are not that many outcomes, this works but
    # eventually there should be an easier way of doing this because you cannot just keep writing
    # IF and ELIF statements for several hundred outcomes.
    # The colon at the end of the statements is important because you are executing something.
    # """

JSON文件配置:

//{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387


    {
        "version": "3.5",
        "configurations": [
            {
                "type": "python",
                "request": "launch",
                "name": "Python: Current File",
                "program": "${file}",
                "console": "internalConsole"
            }
        ]
    }

错误消息输出:

石头(r),纸(p),剪刀(s)? 岩石 不可用

Traceback (most recent call last):

  File "c:\Users\yoush\.vscode\extensions\ms-python.python-2019.4.12954\pythonFiles\lib\python\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_comm.py", line 284, in _on_run
    self.process_net_command_json(self.global_debugger_holder.global_dbg, json_contents)

  File "c:\Users\yoush\.vscode\extensions\ms-python.python-2019.4.12954\pythonFiles\lib\python\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_process_net_command_json.py", line 157, in process_net_command_json
    cmd = on_request(py_db, request)

  File "c:\Users\yoush\.vscode\extensions\ms-python.python-2019.4.12954\pythonFiles\lib\python\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_process_net_command_json.py", line 559, in on_evaluate_request
    py_db, request, thread_id)

  File "c:\Users\yoush\.vscode\extensions\ms-python.python-2019.4.12954\pythonFiles\lib\python\ptvsd\_vendored\pydevd\_pydevd_bundle\pydevd_api.py", line 385, in request_exec_or_evaluate_json
    thread_id, internal_evaluate_expression_json, request, thread_id)

  File "c:\Users\yoush\.vscode\extensions\ms-python.python-2019.4.12954\pythonFiles\lib\python\ptvsd\_vendored\pydevd\pydevd.py", line 873, in post_method_as_internal_command
    self.post_internal_command(internal_cmd, thread_id)

  File "c:\Users\yoush\.vscode\extensions\ms-python.python-2019.4.12954\pythonFiles\lib\python\ptvsd\_vendored\pydevd\pydevd.py", line 877, in post_internal_command
    queue = self.get_internal_queue(thread_id)

  File "c:\Users\yoush\.vscode\extensions\ms-python.python-2019.4.12954\pythonFiles\lib\python\ptvsd\_vendored\pydevd\pydevd.py", line 864, in get_internal_queue
    if thread_id.startswith('__frame__'):

AttributeError: 'NoneType' object has no attribute 'startswith'

2 个答案:

答案 0 :(得分:-1)

在倒数第二行使用else代替elif。它对我有用。

elif user == 'paper' and computerChoice == 'scissors':
    print('You lost!')

elif user == 'scissors' and computerChoice == 'paper':
    print('You won!')

else:
    print('You lost!')

答案 1 :(得分:-1)

我对您使用的IDE不熟悉,但是我怀疑它们之间的区别在于,其中之一是使用python 3来解释您的代码,而另一个是使用python 2.7来解释您的代码。在这两个修订版之间,输入的工作方式存在主要差异:

  • 在python3中,input()函数返回一个字符串(在这种情况下,这是您想要的行为)

  • 在python 2.7中,input()函数将评估用户键入为python命令的内容(很少是您想要的,在这种情况下绝对不是您想要的)。如果要在python 2.7中从用户获取字符串,请改用raw_input()函数。