Reading Inputs, Variables, and Comments In Python

Sari Lakkis
6 min readMay 15, 2021

In this article, we will learn how to read inputs from the users of our app and see how to use variables, data types, casting, and expressions in Python. We are going to learn these topics while solving a small problem:

  1. Read inputs from the user using the input() method
  2. How to store values in variables
  3. how to use comments
  4. Discuss data types
  5. Casting from one data type to another.
Photo by Markus Spiske on Unsplash

We want to solve this small problem: calculating our weekly payment given the number of hours we worked in a week and the hourly wage. Our little application has the following input-output:

Inputs: number of hours, hourly wage

Output: weekly payment

Creating the Project:

Open a command window and create a new folder for our app called “SalaryCalculator” and open VS Code inside this folder. Type the following in your command window:

mkdir SalaryCalculator
cd SalaryCalculator
Code .

you can refer to parts one and two of the series to set up the programming environment for Python and creating a new project.

Reading Input from the User and assigning it to a variable

Python uses the input() method to read input from the user, we can specify a message inside the method to display for the user; for example, in our problem we want the user to input his number of hours worked in a week, so we’ll write:

input("Enter the number of hours worked: ")

Then the user can type the input he wants and press enter, but what will we do with the value we already had from the user? We have to store it in the memory and give it a suitable name to use later in our calculation, we call this container that stores the data value a variable. So, we will assign the value read from the user by the method input to a variable called for example “hours”:

hours = input("Enter the number of hours worked: ")

Let’s test what we’ve written so far by printing back the value of hours to check if the value assigned is correct, so in VS Code in the main.py file type:

hours = input("Enter the number of hours worked: ")
print(hours)

Then click on the Run button, the terminal will be opened at the bottom and the message we wrote inside the input() method is displayed. Now the application is waiting for our input so write any number you want for example 24 and press enter, the value 24 will be displayed by the print method which means that the value 24 was successfully assigned to the variable hours.

Comments in Python

We don’t need to keep the line that prints the hours' value in our application since it was used just for testing the value of the “hours” variable. We might need it later or not so instead of deleting it, we can comment on it by adding the ‘#’ character; which means that this line will not be included in the code and is not going to be read by the compiler.

Comments can be useful in many cases like our case here and also can be used as notes that explain the code to your later self when you get back to a long-time written piece of code or to other developers as well.

There is also another way in Python to comment a whole block of code or multi-line text rather than commenting it line by line; which is using the triple quotes and placing the comments inside it:

Example:

"""
Hey, my fellow developer!
Don't touch my code.
"""

Triple quotes are used for multi-line string literals which are text values like our message to the user or the multi-line comment above.

Reading the Other Input

Let’s read the rate value from the user, the same way as we did for the hours' value:

hours = input("Enter the number of hours worked: ")
#print(hours) this line is not read by the compiler
rate = input("Enter the hourly rate: ")

Now we have both values, we can calculate the weekly payment which is the number of hours multiplied by the hourly rate. We’ll try to calculate it directly using the expression hours * rate inside the print method, add the following line at end of the code.

print(hours * rate)

Click on the Run button and enter the values for hours and rate. What we expected, is to get the result of the expression hours * rate displayed in the terminal but instead, we got this error:

can’t multiply sequence by non-int of type ‘str’

What has happened is that the Python interpreter doesn’t know that the input hours and rate are numbers so it can’t multiply them. This is because Python doesn’t declare variables with types like numeric, text, or others. To fix this error we need to tell the Python interpreter to transform the hours and rate to a suitable data type.

Python Data Types

When we used the input() method the type of the variable hours was set to “str” which is the type used for string or text values. Data types tell the compiler how to represent the data and how to deal with it.

In Python we have many built-in data types, we have seen the “str” for text. For the numerical types, we have int, float, complex.

  • int is used for integer values without decimals like -1, 5
  • float is used for decimals like: 3.14, -0.4
  • complex is used for complex numbers 2 + 3j

A data type is set when a value is assigned to a variable, for example, if we write x = 3.14 then the type of x is set to float or message = “Welcome to Python!” then the type of the message variable is str.

Casting: Transforming from One Type to Another

We may transform from a certain type to another, like in our problem where we want to transform the types of hours and rate variables int, this process is called casting.

To cast the variable, we use the same type name as a method and the variable as the parameter, for example, to cast the hours variable to int we write int(hours) and the same for float and str.

We can also use the same method to construct a type from a literal for example if we want to add float variable tax, that we will add later to our application, then we write

tax = float(0.20)

Completing the Code

Back to our problem, we want to cast hours and rate to the int type and perform the multiplication, we also will store the result in a variable payment:

payment = int(hours) * int(rate)

Then when we want to display the result but we will append a message to the payment value like your payment is $76 so instead of writing print(payment) we will add the “your payment is $” and to add the payment we use the + operator to concatenate the value of payment but as you may have guessed; the type of payment must be str so we’ll cast in using str(payment):

print("your payment is $" + str(payment))

The complete code:

Complete Code of the Problem

Run the program and enter the values you want; the output should be like this:

The Output

Conclusion

In this tutorial, we wrote a little Python program that calculates the weekly payment of an employee based on his number of hours worked and the hourly rate. While solving this problem, we learned how to:

  1. Read inputs from the user using the input() method
  2. How to store values in variables
  3. how to use comments
  4. Discuss data types
  5. Casting from one data type to another.

In the next tutorial, we’ll see the if-statement where we can perform different scenarios in our code based on a certain condition.

--

--

Sari Lakkis

Professor | Consultant | Researcher. I give advice on strategy, design, and implementation of software that solves business problems to companies and startups.