user input output in python

problem

You wish to create a Python program that runs in the console and has some input/ output interaction with the user. For example, it’s asking some input data to be given like name, age, etc. In this post we will see how to accomplish that.

SOLUTION

In Python, we have the built-in function input() that allows us to prompt the user to enter a value in the console. We can store that value in a variable and manipulate it. By default the input() function returns a string.

Let’s see an example:

io.py
				
					name = input("What is your name: ")
print("Hello " + name)

				
			
running
				
					python io.py
				
			

The code above prompts the user to enter their name, it’s storing the string value in the name variable and then it prints a message using string concatenation.

We can see the type of the name variable by using the built-in function type() as follows:

io.py
				
					print(type(name)) #returns the data-type of the variable
				
			
output
type in python

The name is of type class str (string)

Reading numbers from user

What happens if we wish to prompt the user to enter their age that is integer? In this case we can attempt to convert the input to an integer as follows:

io.py
				
					age = int(input("What is your age: ")) #convert to int

				
			

The int() function will attempt to convert the string value to integer. In this case if I enter the value 25, the input will return “25” as a string. The int() function will convert it to numerical 25 so mathematical operators can be applied. 

For example we wish to calculate the year the user was born. We simply subtract the age from the current year.

io.py
				
					current_year = 2050
year_born = current_year - age
print("You were born in " + year_born)
				
			

The code above it’s calculating the year the user was born and then printing it nicely.

 

What happens if the user enters a value that cannot be converted to integer? For example when the following command executes:

io.py
				
					age = int(input("What is your age: "))

				
			

The user enters “20 years old”. Then the program will crash with the following error:

output
valueError python example

This is a ValueError, it cannot convert it to base 10 (digits)

Reading a floating point arithmetic value (float)

We wish to prompt the user for a price. In this case, we can use the built-in float() function that accepts a string parameter and attempts to convert it to its respective floating point value. 

In combination with the input() function it will look as follows:

io.py
				
					price = float(input("Enter price of burger: "))
print("The burger costs " + price)
				
			

Similarly with the int() function, the code above attempts to convert the string value entered by the user into a float. If the value is not convertible then the program crashes with the same type of error as before, i.e. ValueError.

output
ValueError python example float fail

conclusion

In this post we saw how to use the input() function to read data from the user. Moreover, we saw how to convert that input to integer and float by using the int() and float() functions respectively.

Share it!

Facebook
Twitter
LinkedIn
Reddit
Picture of Ellion

Ellion

Professional IT consultant, writer, programmer enthusiast interested in all sorts of coding.
Eats all cookies 🍪

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x