简体   繁体   中英

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

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 !

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! ")) . 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM