【问题标题】:No Idea why my code isn't working (Beginner coder)不知道为什么我的代码不起作用(初学者编码器)
【发布时间】:2019-05-19 17:57:08
【问题描述】:

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

【问题讨论】:

  • 我认为input 引起了一些问题,尝试用硬编码字符串替换它,看看是否有帮助?
  • 您的错误消息输出似乎与您的代码没有任何关系,似乎是由于 IDE 文件中的错误配置而发生的
  • Vasu,你知道我该如何解决这个问题吗?

标签: python visual-studio-code


【解决方案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!')

【讨论】:

  • input() 函数默认返回类型为str,那为什么还要显式地将其转换为字符串呢?
  • 假设你指的是[python-3.x],输入语句返回的数据总是一个字符串值(无编码)。即使您需要编码,那么函数也是data.encode(encoding)
  • 我刚做了这个,但我仍然有同样的问题。
  • 您可能需要改用.encode('utf-8') (b'some_string') 或B'some_string'r'some_string',查看str 编码的python 文档
  • 在哪里进行这些更改?
【解决方案2】:

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

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

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

【讨论】:

  • 刚试过这个,我确定两个IDE都使用Python 3及以上版本,没有一个使用Python 2.7。
猜你喜欢
  • 1970-01-01
  • 2020-09-19
  • 2014-01-17
  • 1970-01-01
  • 2021-04-15
  • 2021-03-11
  • 1970-01-01
  • 2020-02-05
  • 1970-01-01
相关资源
最近更新 更多