CSP1150 Week2 Tutorial
CSP1150 Week2 Tutorial
Programming Principles
Week 2: Practice Exercises
Data Types and Selection
Q1 – Concept Revision in the Python Shell (10 mins)
Let’s revise the concepts from this module by typing some simple statements into the
Python Shell.
• Feel free to deviate from the workshop and experiment!
1. type(-123)
2. type(3.14)
3. type('Example')
4. type(True)
• The type() function tells you the data type of the value or variable that you pass it.
5. int(3.14)
6. float(42)
7. str(123)
8. bool('False')
• These functions are all used to convert data types. Remember, every language follows
slightly different rules when it comes to such conversions – always test your code
thoroughly!
Q1 – Concept Revision in the Python Shell
9. intVal = 10
10. strVal = '5'
11. intVal + strVal
12. intVal + int(strVal)
13. str(intVal) + strVal
• These statements demonstrate combining an integer value and a numeric string –
either by converting the string to an integer, or the integer to a string. The result is
much different.
14. 8 // 5
15. 8 % 5
• You can use “//” if you want to do integer division, and “%” (known as the "modulo
operator) to determine the remainder of a division. These can come in handy
sometimes!
Q1 – Concept Revision in the Python Shell
16. 'Fish' * 5
17. '10' * 5
• While adding, subtracting or dividing a string doesn’t make much sense, Python allows
you to multiply strings – this can be useful, but also confusing if you have a numeric
string.
• Try to determine the final True/False result of these boolean expresisons before typing
them.
18. False or False or not False and True
19. not(True and False) or False
Questions 2, 3, and 4
The program should also display a message indicating whether the person has optimal
weight, is underweight, or is overweight. A person’s weight is considered to be optimal if
his or her BMI is between 18.5 and 25. If the BMI is less than 18.5, the person is
considered to be underweight. If the BMI value is greater than 25, the person is
considered to be overweight.
Q3 – Grade Calculation Example (20 mins)
A class has two tests worth 25 points each along with a main exam worth 50 points. For a
student to pass the class, they must obtain an overall score of at least 50 points, and must
obtain at least 25 points in the main exam. If a student’s total score is less than 50 or they
obtain less than 25 points in the main exam, they receive a grade of “Fail”. Otherwise,
their grade is as follows:
• If they get more than 80, they get a grade of “Distinction”. 50–59 = “Pass”.
• If they get less than 80 but more than 60, they get a “Credit” grade.
• If they get less than 60, they get a ”Pass” grade.
Write a program that prompts the user to enter their points for both tests and the exam.
The program should first check if the points entered for the tests and exam are valid. If
any of the test scores are not between 0 and 25, or the exam score is not between 0 and
50, the program should display an error message. Otherwise, the program should display
the total points and the grade.
Part B: The above class is allowed to take an additional quiz (optional) which is worth 10
points (which makes the total 110), the marks of this quiz is added to the final grade, only
if their total marks are less than 50 (2 tests + main exam). Modify the above code to
prompt the user a question asking if they took this optional quiz by answering yes(y) or
no(n), (the code should work for both lower and upper case letters), if so, enter the marks
of this quiz out of 10. Recalculate the total points and the grade.
Q4 – Package Discount Program (20 mins)
A software company sells 2 types of packages namely package A & package B that retails
for $99 and $49. Quantity discounts are given according to the following tables:
Part A: Assume that a customer must buy both type of packages. Write a program that
asks the user to enter the number of packages purchased. The program should then
display the amount of the discount for both packages seperately (if any) and the total
amount of the purchase after the discount.
Part B: Now assume that a customer has the option to select between buying one of
these packages (A or B) or buying both (A and B). Write a program to ask the user to
select their options and calculate the total discount and the total amount to be paid after
discount.