在Vscode终端运行编码器时,它说我的变量不确定
我在vscode中写了一个简单的代码,但是当我在终端中运行它时,错误会弹出错误,说该变量未定义。但是,当我手动按“运行代码”按钮时,代码无任何错误运行。
代码:
name = input("what is your name? ")
print("hello,", name)
错误消息:
$ python hello.py
what is your name? ted
Traceback (most recent call last):
File "hello.py", line 1, in <module>
name = input("what is your name? ")
File "<string>", line 1, in <module>
NameError: name 'ted' is not defined
I wrote a simple piece of code in VScode but when I run it in the terminal an error pops up saying the variable is not defined. however when I manually press the run code button the code runs without any errors.
The code:
name = input("what is your name? ")
print("hello,", name)
the error message:
$ python hello.py
what is your name? ted
Traceback (most recent call last):
File "hello.py", line 1, in <module>
name = input("what is your name? ")
File "<string>", line 1, in <module>
NameError: name 'ted' is not defined
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您在计算机上安装了多个Python版本,并且计算机的默认Python版本应为
Python2
(也许与系统环境变量有关)。您可以使用py -v
检查版本。建议您使用虚拟环境,并且在每个虚拟环境中仅保留一个Python版本。当您选择解释器时( ctrl + shift + p - &gt; python:选择解释器),新的终端( ctrl + shift + `)将自动激活虚拟环境。
虚拟环境对您使用解释器和特定软件包的特定版本非常有用。
如何创建虚拟环境:
创建虚拟环境
激活虚拟环境
官方文档:创建一个虚拟环境
It looks like you have multiple python versions installed on your machine, and your computer's default python version should be
python2
(perhaps related to system environment variables). You can check the version withpy -V
.It is recommended that you use a virtual environment and keep only one python version in each virtual environment. When you select an interpreter ( Ctrl + Shift + P --> Python: Select Interpreter), the new terminal ( Ctrl + Shift + ` ) will automatically activate the virtual environment.
Virtual environments are very useful for you to use a specific version of the interpreter and a specific package.
How to create a virtual environment:
Create Virtual Environment
Activate Virtual Environment
official documentation: Create a virtual environment