Description
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”
print (“a”,”b”,”c”, sep=”|”)
displays “a|b|c”
Question 1 [20 marks]
ASCII Art is a technique to create fancy effects using simple text output.
By using only print statements, write a program called ‘rabbit.py’ that outputs the following:
(\\ (\/)
( ”) (\_/) (.. ) //)
O(“)(“) (\’.’/) (“)(“)O (” )
(“)_(“) ()()o
Do not include the blue guides – these are there simply so you can see how many spaces are
between characters.
Here’s the raw text for further reference:
(\\ (\/)
( ”) (\_/) (.. ) //)
O(“)(“) (\’.’/) (“)(“)O (” )
(“)_(“) ()()o
Some of the backslashes (\) may need to be escaped (written as \\ instead). Search online to find out
why this is necessary.
Question 2 [25 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
It is with a heavy heart that I inform you of the death of my father,
General Fayk , your long lost relative from Mapsfostol.
My father left the sum of USD for us, your distant cousins.
Unfortunately, we cannot access the money as it is in a bank in .
I desperately need your assistance to access this money.
I will even pay you generously, 30% of the amount – USD,
for your help. Please get in touch with me at this email address asap.
Yours sincerely
Frank
Sample IO
Enter first name:
Patrick
Enter last name:
Star
Enter sum of money in USD:
1234
Enter country name:
South Africa
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 3 [25 marks]
Write a program called ‘time.py’ for checking the validity of a time entered by the user as a set of
three integers. In professional software, it is never assumed that input from users is valid so you too
need to do this in your programs.
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).
Three samples of program behaviour:
1)
Enter the hours: 21
Enter the minutes: 7
Enter the seconds: 7
Your time is valid.
2)
Enter the hours: 25
Enter the minutes: -7
Enter the seconds: 0
Your time is invalid.
3)
Enter the hours: 21
Enter the minutes: 7
Enter the seconds: 72
Your time is invalid.
Question 4 [30 marks]
Given sides of length a, b, c, the area of a triangle may be calculated as follows (Heron’s formula):
s=(a+b+c)/2
Area=sqrt(s*(s-a)*(s-b)*(s-c))
Write a program called ‘triangle.py’ that asks the user to enter the lengths of the three sides of
a triangle, and that calculates and prints the area.
Sample IO:
Enter the length of the first side: 3
Enter the length of the second side: 4
Enter the length of the third side: 5
The area of the triangle with sides of length 3 and 4 and 5 is 6.0.
HINT: Say you wanted to find the square root of 121 – how would you go about this? In Python (and
many other languages), there are built-in functions that can perform such calculations for you. The
function to find a square root is ‘math.sqrt’ (‘sqrt’ in the ‘math’ library).
Submission
Create and submit to the automatic marker a Zip file called ABCXYZ123.zip (where ABCXYZ123 is
YOUR student number) containing rabbit.py, spam.py, time.py and triangle.py.
END