Introduction to Python

Input integers

So far we have worked with input() textual values (strings). But what if we want to interact with numbers instead of text? Indeed input() ”reads” text from the input, so Python needs to know if we would like to treat the inputted text as a number. That can be accomplished with int():
some_text = input()
num1 = int(input())
num2 = int(input())
print('The text was:', some_text)
print(num1, num2, num1 + num2)
# Sample input:
Hi, my age and height are:

# Sample output:
The text was: Hi, my age and height are:
24 180 204
int stands for integer (whole number). int(...) converts the inputted text to an integer. So, when the user inputs 20, it is converted to a number 20 and then can be used for addition, multiplication, etc.
If the user inputs a wrong value, let's say hello instead of a number num1 (24 in the example above), Python would try to convert it to int and would result in an error telling that it wasn’t able to do so ValueError: invalid literal for int() with base 10: 'hello'. ValueError indicates that there was a wrong value provided and the program is not able to treat the input as a number.


Write a program that inputs a number n and prints The number you have entered is: and its value.
The number you have entered is: 10
The number you have entered is: -4


Time limit: 1 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue