简体   繁体   English

Python/终端 - 为什么我在尝试获取用户输入时收到此错误消息

[英]Python/Terminal - Why am I getting this error message when I try to get user input

In VS code, I enter the following code:在 VS 代码中,我输入以下代码:

print("Let me help you add 2 numbers")
first_number = int(input("Enter your first number! "))
second_number = int(input("Enter your second number! "))
print("The total is", first_number + second_number)

Because I am using VS code, I can only collect user inputs in the terminal.因为我使用的是 VS 代码,所以只能在终端收集用户输入。 However, this is the error message I receive:但是,这是我收到的错误消息:

>>> print("Let me help you add 2 numbers") Let me help you add 2 numbers
>>> first_number = int(input("Enter your first number! ")) Enter your first number! second_number = int(input("Enter your second number! "))
 Traceback (most recent call last):   File "<stdin>", line 1, in
 <module> ValueError: invalid literal for int() with base 10:
 'second_number = int(input("Enter your second number! "))'
>>> print("The total is", first_number + second_number) Traceback (most recent call last):   File "<stdin>", line 1, in <module>
NameError: name 'first_number' is not defined
>>>

Why am I receiving this error and why can't I just collect user input?为什么我会收到此错误,为什么我不能只收集用户输入?

Your user input for the second line of code inside terminal was actually the third line of the code.您在终端内的第二行代码的用户输入实际上是代码的第三行。

The code just failed trying to type cast this whole line of code second_number = int(input("Enter your second number! ")) into integer.代码尝试将整行代码second_number = int(input("Enter your second number! "))输入 integer 时失败。

Try running one line at a time inside the terminal in stead of copy pasting the whole code at once.尝试在终端内一次运行一行,而不是一次复制粘贴整个代码。 That should work !那应该工作!

this is how you should have done it这就是你应该做的

在此处输入图像描述

This was your mistake for pasting hole Third line when it was actually asking for user input of first number这是您粘贴孔第三行实际上要求用户输入第一个数字时的错误

在此处输入图像描述

here your third line of code second_number = int(input("Enter your second number! ")) is taken as the input for the second line first_number = int(input("Enter your first number! ")) .在这里,您的第三行代码second_number = int(input("Enter your second number! "))作为第二行的输入first_number = int(input("Enter your first number! ")) in the terminal, try giving one line of code,after it executes, the other.在终端中,尝试给出一行代码,在它执行后,另一行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么每次尝试运行 VS Code python 终端时都会出现语法错误? - Why am I getting a syntax error whenever I try to run VS code python terminal? 为什么我会收到此错误消息? Python - Why am I getting this error message? Python 当我尝试将mwclient模块用于Python时,为什么会出现错误“无法导入名称扫描仪”? - Why am I getting the error “cannot import name Scanner” when I try to use the mwclient module for Python? 每当我尝试运行它时,我都会收到一条错误消息 - I am getting an error message whenever I try and run this 为什么我收到以下错误消息? - Why am I getting the following error message? 当我尝试引用以前在Python中定义的文件变量时,为什么会出现NameError? - Why am I getting a NameError when I try to reference to a file variable I have previously defined in Python? 当我将包含 integer 的变量与包含来自用户的数字输入的变量等同起来时,为什么会出现语法错误? - Why am I get a syntax error here when I am equating a variable containing an integer and a variable containing a numerical input from the user? Python/Discord 当我尝试让 python 获取不和谐用户的用户 ID 时,它什么也不做,没有错误消息,也没有输出 - Python/Discord When I try to make python get the userID of a discord user it just does nothing, no error message and no output 尝试运行Pyramid时为什么会出现ImportError? - Why am I getting an ImportError when I try to run Pyramid? 当我尝试运行Pyramid项目时,为什么会出现DistributionNotFound错误? - Why am I getting DistributionNotFound error when I try to run Pyramid project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM