IO, Control (If)
IO, Control (If)
Assignment Instructions
This assignment involves constructing Python programs that use input and output statements, 'if'
and 'if-else' control flow statements, and statements that perform numerical manipulation.
You may need to use additional attributes of the print statement that control what is printed at the
end of each print statement (end="\n") and separating each value in a list of values (sep=" ").
For example:
print ("a","b","c")
displays "a|b|c"
NOTE Your solutions to this assignment will be evaluated for correctness. Assignments that follow
(starting with Assignment 4) will also be evaluated for the following qualities:
• Documentation
o Use of comments at the top of your code to identify program purpose, author and
date.
o Use of comments within your code to explain each non-obvious functional unit of
code.
• General style/readability
o The use of meaningful names for variables and functions.
• Algorithmic qualities
o Efficiency, simplicity
These criteria will be manually assessed by a tutor and commented upon. In future assignments, up
to 10 marks will be deducted for deficiencies.
By using only print statements, write a program called 'com_sci.py' that outputs the following:
Page 1 of 4
Do not include the blue guides - these are there simply so you can see how many spaces are
between characters.
- - - - - - - - - - - - - - - - - - - - - - - - -
/ - - - / - - - | - - / - - - | | - - - | | | | \ | | |
| | \ - - - \ | | \ - - - \ | | - | | | | \ | | |
| | - - - - - - ) | | - - - ) | - | | | - | | | \ | - |
\ - - - - | - - - - / - - - | - - - / | | \ - - - / | - | \ - ( - )
Some of the backslashes (\) may need to be escaped (written as \\ instead). Search online to find out
why this is necessary.
In this case, you want to check if the number of hours is between 0 and 23 (inclusive), the number of
minutes is between 0 and 59 (inclusive) and the number of seconds is between 0 and 59 (inclusive).
Sample I/O (The input from the user is shown in bold font):
Enter the hours:
21
Enter the minutes:
7
Enter the seconds:
7
Your time is valid.
Sample I/O (The input from the user is shown in bold font):
Enter the hours:
25
Enter the minutes:
-7
Enter the seconds:
0
Your time is invalid.
Sample I/O (The input from the user is shown in bold font):
Enter the hours:
21
Enter the minutes:
7
Enter the seconds:
72
Your time is invalid.
Question 3 [15 marks]
Write a program called ‘spam.py’ to generate a personalised spam message based on the user's full
name, country and a sum of money. Use the following template for the spam message, with a blank line
before the message starts.
Dearest <first_name>
It is with a heavy heart that I inform you of the death of my father,
General Fayk <last_name>, your long lost relative from Mapsfostol.
My father left the sum of <money>USD for us, your distant cousins.
Unfortunately, we cannot access the money as it is in a bank in <country>.
I desperately need your assistance to access this money.
I will even pay you generously, 30% of the amount - <money30>USD,
for your help. Please get in touch with me at this email address asap.
Yours sincerely
Frank <last_name>
Dearest Patrick
It is with a heavy heart that I inform you of the death of my father,
General Fayk Star, your long lost relative from Mapsfostol.
My father left the sum of 1234USD for us, your distant cousins.
Unfortunately, we cannot access the money as it is in a bank in South Africa.
I desperately need your assistance to access this money.
I will even pay you generously, 30% of the amount - 370.2USD,
for your help. Please get in touch with me at this email address asap.
Yours sincerely
Frank Star
Hint: Use "\n" at the end of your input string to move to the next line before input.
Question 4 [20 marks]
Write a program called leapYear.py to determine whether a year is a leap year or not. A year is a
leap year if (a) it is divisible by 400 or (b) it is divisible by 4 but not by 100. Your program must accept
input from the user in the form of an integer representing the year. Then, it checks if it is a leap year
or not.
Sample I/O (The input from the user is shown in bold font):
Enter a year:
2023
2023 is not a leap year.
Sample I/O (The input from the user is shown in bold font):
Enter a year:
2020
2020 is a leap year.
Submission
Create and submit to the automatic marker a Zip file called ABCXYZ123.zip (where ABCXYZ123 is
YOUR student number) containing com_sci.py, spam.py, time.py and leapYear.py.
NOTES: