You are on page 1of 8

Hong Kong Institute of Vocational Education

FS113002N Diploma of Foundation Studies – Information Technology


ITE3711 Programming Concept and Applications
Topic 3
Lab 3.2 – Input and Functions

Exercise 1
Write a Python program to check the age of the user.

Data flow and functions of the program:


 User input the age
 The program pass the age to the function named checkAge and check
Age Return message
<0 Invalid
<18 Not allow to vote
>=18 Allow to vote
 Print the message

Sample execution - * Bold and italics are user inputs


PS C:\Users\itstaff> python main.py
Enter your age: 29
Allow to vote

PS C:\Users\stitstaff> python main.py


Enter your age: -5
Invalid
Answer:
Exercise 2
Write a Python program to check the weather.

Data flow and functions of the program:


 User input the temperature
 The program pass the temperature to the function named checkTemp and check
Temperature Return message
<0 It’s freezing outside
0 - 10 It’s quite cold outside
11 – 20 It’s a bit chilly outside
21 – 30 It’s a comfortable temperature outside
Others It’s quite hot outside
 Print the message

Sample execution - * Bold and italics are user inputs


PS C:\Users\itstaff> python main.py
Enter the temperature (Celsius): 29
It's a comfortable temperature outside

PS C:\Users\stitstaff> python main.py


Enter the temperature (Celsius): 8.5
It's quite cold outside

Answer:
Exercise 3
Write a Python program to check the number’s value.

Data flow and functions of the program:


 User input the number
 The program pass the number to the function named checkNumber and check
Number Return message
<0 The number is negative
==0 The number is equal to zero

!=0 Go to another if statement


If the number %2 == 0
Result Return message
True The number is even and positive
False The number is odd and positive

 Print the message

Sample execution - * Bold and italics are user inputs


PS C:\Users\itstaff> python main.py
Enter a number: -5
The number is negative

PS C:\Users\stitstaff> python main.py


Enter a number: 0
The number is equal to zero

PS C:\Users\stitstaff> python main.py


Enter a number: 8
The number is even and positive
Answer:
Exercise 4
Write a Python program to calculate the cost of Cola.
Data flow and functions of the program:
 User input the number which is the volume of the Cola
The program pass the number to the function named CalCost and calculate the cost of Cola with
following formula

Cost = Volume * 15
E.g:
print(CalCost(100)

The Output should be:


1500

Sample execution - * Bold and italics are user inputs


PS C:\Users\itstaff> python main.py
Enter the volume of Cola: 100
$1500

PS C:\Users\stitstaff> python main.py


Enter the volume of Cola: 0
$0

PS C:\Users\stitstaff> python main.py


Enter the volume of Cola: 5
$75
Answer:

You might also like