Making Calculations from User Input with Python

30 minutes
  • 4 Learning Objectives

About this Hands-on Lab

After learning about data structures, user input and output programming becomes a lot more powerful. There are many things that we can accomplish by simply taking user input, running it through a process, and returning some useful output. To put these skills to use, we’ll write a simple tool to perform temperature conversions from Fahrenheit to Celsius.

To feel comfortable completing this lab you’ll want to know how to do the following:

* Handling user input using the `input` function. Watch the videos from the “Input and Output Operations” section of the Certified Entry-Level Python Programmer Certification course.
* Type casting from strings to floats. Watch the “Type Casting” video from the Certified Entry-Level Python Programmer Certification course.
* Printing to the screen. Watch the videos from the “Input and Output Operations” section of the Certified Entry-Level Python Programmer Certification course.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create the ~/to-celsius.py Script and Make It Executable with python3.7

For the time being, we’re going to write our script in our home directory (~) and we want to be able to run it right away. To make sure that we’re not completely tied to the path of our python3.7 binary, we need to we set up our shebang properly.

Let’s create the file and set the shebang:

~/to-celsius.py

#!/usr/bin/env python3

# Python implementation here

Now that the file is created, we need to also make it executable by using chmod:

$ chmod u+x ~/to-celsius.py

Now we can run our script by running ./to-celsius.py from our home directory.

Prompt and Store Fahrenheit Value from User

When we run our script, we would like to prompt the user for a temperature value in Fahrenheit. Let’s do this using the input function:

~/to-celsius.py

#!/usr/bin/env python3

# Python implementation here
fahrenheit = input("What temperature (in Fahrenheit) would you like converted to Celsius? ")

By default, this variable will be a string, so we’ll need to cast it to be a float:

~/to-celsius.py

#!/usr/bin/env python3

# Python implementation here
fahrenheit = float(input("What temperature (in Fahrenheit) would you like converted to Celsius? "))

Now we’re ready to use this value in our calculation.

Calculating the Celsius Value

The Fahrenheit to Celsius formula is celsius = (temp - 32) * 5 / 9 and this converts almost perfectly to Python. We only need to change temp to be our variable fahrenheit.

~/to-celsius.py

#!/usr/bin/env python3

# Python implementation here
fahrenheit = float(input("What temperature (in Fahrenheit) would you like converted to Celsius? "))
celsius = (fahrenheit - 32) * 5 / 9
Print the Calculated Value to the Screen

Now that we have our final value we’re ready to print it to the screen in the form of:

FAHRENHEIT F is CELSIUS C

We can do this by passing our values into the print function:

~/to-celsius.py

#!/usr/bin/env python3

# Python implementation here
fahrenheit = float(input("What temperature (in Fahrenheit) would you like converted to Celsius? "))
celsius = (fahrenheit - 32) * 5 / 9
print(fahrenheit, "F is", celsius, "C")

Now if we use our script we should see the following:

$ ./to-celsius.py
What temperature (in Fahrenheit) would you like converted to Celsius? 68.18
68.18 F is 20.100000000000005 C

We’re showing more decimal places in our Celsius value than we might like and these can be hidden by using the round function to round to 2 decimal places like
this round(celsius, 2).

Additional Resources

We have international coworkers, and there's no topic of conversation more common than the weather. This simple script will be quite useful to us, providing a quick reference to check in the middle of any "How is the weather over there," types of conversations. Here is what the final process (running the script) will look like:

$ ./to-celsius.py
What temperature (in Fahrenheit) would you like converted to Celsius? 68.18
68.18 F is 20.1 C
$

Logging In

Using the Terminal To Complete the Lab

There are a couple of ways to get in and work with the code. One is to use the credentials provided in the lab, log in with SSH, and use a text editor in the terminal, such as Vim.

Note: When copying and pasting code into Vim from the lab guide, first enter :set paste (and then i to enter insert mode) to avoid adding unnecessary spaces and hashes. To save and quit the file, press Escape followed by :wq. To exit the file without saving, press Escape followed by :q!.

Using VS Code To Complete the Lab

You can also access the lab using VS Code in the browser. If you'd like to go this route, then follow the steps below:

  1. Navigate to the public IP address of the workstation server (provided in your lab credentials) on port 8080, using http (e.g., http://PUBLIC_IP:8080).
  2. If you receive a notification indicating the connection is not secure, click Advanced. Then, proceed to the server.
  3. Use the password provided in your lab credentials.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?