Reputation: 87
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?
Upvotes: 0
Views: 439
Reputation: 1
Your third line of code
second_number = int(input("Enter your second number! "))
Is actually taken as the input for the second line
first_number = int(input("Enter your first number! "))
.
In the terminal, try executing one line of code at a time.
Upvotes: 0
Reputation: 167
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
Upvotes: 0
Reputation: 6935
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 !
Upvotes: 1