Module 1 - Section 2 - Input and Sequential Flow

Site: ΕΛ/ΛΑΚ Moodle
Course: Python Lab
Book: Module 1 - Section 2 - Input and Sequential Flow
Printed by: Guest user
Date: Tuesday, 7 May 2024, 12:46 PM

Description

After the completion of this module the students will be able to:

  • use the input command
  • construct commands to get data from input and store them in variables
  • differentiate variable data types
  • use commands to convert data types

1.2.1 Scripts

Until now we have learned that we can interact with Python using the CLI interface. But entering Python code at the >>> prompt is slow and can only be done one line at a time. It is also not possible to save the code so that another person can run it. Thankfully, there is an even better way to enter Python code.

Python code can be entered using a script. A script is a series of lines of Python code that will be executed all at once.

In Thonny you can write a series of commands in the editor window and save them as a script.

scripts


To create a simple script copy the following code to the script area and save it as my_first_script.

Press the play button to view the results of the script.

1.2.2 - Data type conversion

In the previous example we have read from the input and assign what we have read to a variable.

In the case of the age input is something preventing as from writting the age with letters?

Try this in CLI

>>> a = input("Enter a number : ")

>>>

Sixteen

>>> 

print (a)

How can we find what type is a variable? We can use the command type. Try the following code sequentially in the command line entering a floating point number (ex. 34.5).

>>> a = input("Enter a number : ")

>>> type (a)

As you can view variable a is a string. You must remember that anything that it is read from the input without conversion is treated as a string. To convert a string to an integer or to a floating point number we can use the int() or float() data conversion commands.

Now try the following commands sequentially:

>>> a = input("Enter a number : ")

>>> 17

>>> print (a)

>>> print(a+2)

The previous command produces an error. Why? Find out usind the following commands.

>>> type(a)

>>> a = int(a)

>>> type(a)

>>> print(a)

>>> print(a+2)

>>> a = float(a)

>>> type(a)

>>> print(a+5)

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