Module 1 - Section 2 - Input and Sequential Flow
Module 1 - Section 2 - Input and Sequential Flow
1.2.3 - Kilometers per Liter
Let's say that we would like to calculate the kilometers per liter that a car achieves.
A simple program would be :
Copy the code to Thonny's scripting area and save it in your working space as kpl.py
This script could be even more useful if it would interact with the user asking the users to input the variables. This can be done with the input statement. We will also use better name for the variables so we would not need comments to explain what the variables are representing. See the code below.
Why this code produces an error? This is because the program does not know the user will be entering numbers. The user might enter “Bob” and “Mary”.
To tell the computer these are numbers, it is necessary to surround the input function with an int( ) or a float( ). Use the former for integers, and the latter for floating point numbers. Copy the following code to Thonny's scripting area and save it as kpl_v1.0.py