In VS code, I enter the following code:
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. 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.
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! "))
. in the terminal, try giving one line of code,after it executes, the other.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.