Simple python program with user input

Last updated : Jul 30, 2023 12:00 AM

Reading user input in Python is 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. To use our program stand-alone, we have to save our code as a python file. Here are the steps.

  1. Open your Python idle console by running the command idle3.10.
    Figure 1 : Python Idle console
    Figure 1 : Python Idle console

  2. Click File -> New File. That will open a new editor.
  3. Click File -> Save As and save the file with a name.

I 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.

Figure 2 : Editing our user-input.py
Figure 2 : Editing our user-input.py

How to read user input in Python?

Reading user input in Python is simple with the input() function.

something = input("Please enter something: ")

The input function accepts a string argument to print on the screen when 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 variable's data type is string. In other words, my variable something's data type is string. But I need int or float data type to calculate the area. I 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 calculate 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))

Here is the complete code.

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.

In my code example, I have
print('Your %s area is: %s' %(shape, area))
The shape value replaces the first %s, and the area value replaces the second %s.

Here are some primary placeholders 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

Hi, I'm Lance Raney, a dedicated Fullstack Developer based in Oklahoma with over 15 years of exp

Read more...