[英]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 !
那应该工作!
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.