Inputting an integer (int)
input() function returns everything as a string, so you need to do an explicit conversion to get an integer. The
int() function comes in handy for this.
# outputs the sum of two numbers entered by the user num_1 = int(input("Enter first number: ")) num_2 = int(input("Enter second number: ")) print("Type num_1:", type(num_1)) print("Type num_2:", type(num_2)) result = num_1 + num_2 print("Sum of the entered numbers:", result)
int(string) converts the passed string to an integer. Entering a number float You can use the
float() function by analogy.
float_1 = float(input("Enter a number: ")) print("Type float_1:", type(float_1)) result = float_1 ** 2 print("Number squared:", result)
Conclusion of the example:
Enter a number: 1.8 Type float_1: Number squared: 3.24
Entering numbers into a string with a space
But what happens if you don’t know the number of input elements? Suppose you want to get a list of numbers and return the sum of the numbers. At the same time, you don’t know the number of elements in that list. So how do you ask for input for it? You can use the
map functions to do this. The
split() method splits the entered string into a list of substring numbers. Then
map() performs the
int() function for each element in the list.
entered_list = input("Enter a list of numbers separated by a space: ").split() print("Entered list:", entered_list) num_list = list(map(int, entered_list)) print("List of numbers: ", num_list) print("List sum:", sum(num_list))
Enter a list of numbers separated by a space: 1 34 4 6548 Enter list: ['1', '34', '4', '6548'] List of numbers: [1, 34, 4, 6548] List sum: 6587
In the code above:
input()returns a list containing numbers separated by commas.
split()returns a list of strings separated by spaces.
int()operation on all list elements and returns a
mapobject back to a list.
There is an alternative way to get the list:
entered_list = input("Enter a list of numbers separated by a space: ").split() num_list = [int(i) for i in entered_list] print("List of numbers: ", num_list)
Handling user input errors There is often a
ValueError exception thrown when converting types. This occurs when the data entered by the user cannot be converted to a particular type. For example, the user enters a random string as age.
num = int(input("Enter age: "))
int() function expects an integer value wrapped in a string. Any other value will cause an error. This is what happens if you try to type “Twenty”:
Enter age: Twenty --------------------------------------------------------- ValueError Traceback (most recent call last) in ----> 1 num_1 = int(input('Enter Age: ')) ValueError: invalid literal for int() with base 10: 'Twenty'
To make sure that the user only enters the correct information, we need to handle a lot of these errors. To do that we will use exception catching.
try: num = int(input("Enter a number: ")) print("That's right. The number:", num) except ValueError: print("This is not a number.")
Let’s see how entering “Twenty” will work now:
Type in a number: Twenty This is not a number.
In this example, if the user enters a nonnumeric value, an exception is raised. However, it is intercepted by the
except instruction, which responds with, “This is not a number. By using the try-except construct, the program won’t stop if the user enters an invalid value.