首页 > 解决方案 > 不知道为什么我的代码不起作用(初学者编码器)

问题描述

我使用 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'

标签: pythonvisual-studio-code

解决方案


在倒数第二行使用 else 而不是 elif。它对我有用。

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

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

else:
    print('You lost!')

推荐阅读