Home > Courses > Python > Introduction to Python

Simple python program with user input

Last updated : December 25, 2020

Reading user input in Python is made simple with the input() function. The input() function reads user input data and stores it on a variable. Our goal is to create a simple python program to get input from the user, do some processing with it, and print the result back to the screen. To accomplish that, we will use functions input() and print() along with other arithmetic operations.

What do we create?

We will create a python program to accept the width and height of a rectangle and calculate the area and perimeter.

Creating a python script

Writing our code directly in python idle console is easy, but less useful. In order for us to use our program stand-alone, we have to save our code as a python file.

Open your python idle console.

Python Idle console
Figure 1 : Python Idle console

Click File -> New File

This will open a new editor. Click File -> Save As and save the file with a name. We will use user-input as the file name. The saved file will be user-input.py. This will be our code editor throughout this tutorial.

Python Idle file editor
Figure 2 : Editing our user-input.py

How to read user input in python?

Reading user input in python is simple with the use of the input() function.

something = input("Please enter something: ")

The input function accepts a string argument to print at the time of prompting user input.

>>> something = input("Please enter something: ")
Please enter something: Learning Python
>>> something
'Learning Python'

Note that when you use input() to assign a variable, the data type of the variable will be cast to a string. In the above case, it would be type string. We need an int or float to calculate the area. To achieve this, the input can typecast the user input value to float in a single line.

width = float(input('Please enter the width: '))
height = float(input('Please enter the height: '))
area = width  * height
perimeter = 2 * (width + height)
print("Your rectangles area is: ", area )
print("Your rectangles perimeter is: ", perimeter )

The first two lines accept the width and height of the rectangle. Lines 3 and 4 calculates the area and perimeter.

Lines 4 and 5 print the calculated values on the screen using the print() function.

To run the program, click Run -> Run Module from the window menu or press F5.

Please enter the width: 24
Please enter the height: 10
Your rectangles area is:  240.0
Your rectangles perimeter is:  68.0

Let's make our program more interactive

We can use the input() function to make our program execution more interactive. Let's modify our program by prompting for a shape the user wants to calculate the area of (although we don't support all shapes yet).

shape = input('Please enter the shape you want to calculate the area of: ')

Now we can use the user entered shape to prompt further inputs necessary to do our calculation.

width = float(input('Please enter the width of your %s: ' %(shape )))
height = float(input('Please enter the height of your %s: ' %(shape )))

Similarly, let's apply the same to our screen prints.

print('Your %s area is: %s' %(shape, area))
print('Your %s perimeter is: %s' %(shape, perimeter))

To make things clear, the full enhanced program is listed below.

shape = input('Please enter the shape you want to calculate the area of: ')
width = float(input('Please enter the width of your %s: ' %(shape )))
height = float(input('Please enter the height of your %s: ' %(shape )))
area = width  * height
perimeter = 2 * (width + height)
print('Your %s area is: %s' %(shape, area))
print('Your %s perimeter is: %s' %(shape, perimeter))
Please enter the shape you want to calculate the area of: Rectangle
Please enter the width of your Rectangle: 24
Please enter the height of your Rectangle: 10
Your Rectangle area is: 240.0
Your Rectangle perimeter is: 68.0

Python uses C-style formatting to format strings. The % is used to format a variable or a tuple of variables (%shape or %(shape, perimeter)), along with a format string. %s place holder is replaced with the designated string value.

Below are some basic place holders we commonly use:

  • %s - string (or any object that can be cast to a string)
  • %d - integers
  • %f - float
  • %.f - Floating point numbers with a fixed number of decimals
  • %x/%X - Integers in hex representation
Lance
By: Lance
Lance is a software engineer with over 15 years of experience in full-stack software development.
Read more...

Comments are disabled

No Comments