Python 2.4

Temperature Converter

  • Fully automatically graded on code quality, style, structure and functionality
  • Students write their solution in an online IDE in their LMS
  • Check for Python code plagiarism

Pre-requisites:

  • Print statement.
  • Variables.
  • String methods.
  • Input function.

Learning goals:

  • Put input statements into practice.
  • Learn how to use conditional statements (if / else).
  • Put string methods into practice.
Get access to an autograder, plagiarism checker and online IDE for this assignment now!

Task:

Since 1960, the International System of Units (or SI, short for Système international) has been in place. This system is used in nearly every country, standardizing the units of measurement for science, technology, industry and everyday life. In this system, there are standard units for measurements for amongst others time (seconds), length (meters), electric current (ampere) and temperature.

Where most quantities have only one unit of measurement, temperature is a little different. The SI prescribes that temperature has the unit of measurement of Kelvin, but in practice it is common to use other units of measurement. In most of the world, Celsius (centigrade) is preferred in everyday life as this allows for more manageable and understandable numbers than Kelvin.

It is thus very important to be able to convert temperature measured in Kelvin to Celsius, and what better way to do this than to use Python! Whereas Kelvin’s zero point (0 K) is defined by the absolute zero of temperature, Celsius’ zero point (0 °C) is defined by the freezing point of water. In Celsius, the absolute zero of temperature is −273.15 °C, which is also the offset between the two scales.

Now that we know how to ask the user for input, let’s create a script `kelvin_to_celsius.py` that asks the user to input a temperature in Kelvin and outputs that temperature in Celsius.

You have hopefully already seen that once we start asking the user to give input, there are many more things that can go wrong. It is always possible that a user gives an input you did not expect and that is not compatible with the code you have written. If we do not check if the user inputs the right thing (in our case, a number), the script will crash. To prevent this we need to implement something called input validation, a crucial part of the security and functionality of almost any application with user input. Implement basic input validation using a string method and an if-statement to check whether the input was actually a digit. If not, fail gracefully by showing the user the error `“Please input a digit.”`.

Example usage:

-!- CODE language-bash -!-$ python3 kelvin_to_celsius.py
Temperature in Kelvin: 0
- 273.15

$ python3 kelvin_to_celsius.py

Temperature in Kelvin: 500
226.85

$ python3 kelvin_to_celsius.py

Temperature in Kelvin: word
Please input a digit.

Learn more:

Start teaching Python now

CodeGrade is your virtual teaching assistant for your coding classroom. You teach, CodeGrade takes care of the rest.

With our 40+ built-in fully automatically graded Introduction to Python assignments for university-level courses, you can start teaching Python at your institution with confidence now. Or, scale your classroom with CodeGrade's tools.

Book a personal 30-minute demo now and we'll show you how to get started with CodeGrade in less than 1 hour!

Book a demo now!

Happy educators

  • Educators who use CodeGrade have less stress.
  • Educators who use CodeGrade have happier and more successful students.
  • Educators who use CodeGrade have a simpler teaching workflow.

Schedule a personalized tour of CodeGrade today.