0% found this document useful (0 votes)
4 views8 pages

CSP1150 Week2 Tutorial

The document outlines practice exercises for a programming principles course focusing on data types and selection in Python. It includes tasks for concept revision using the Python Shell, programming assignments for calculating BMI and grades, and a package discount program. Each question requires pseudocode, flowcharts, and Python code implementation.

Uploaded by

ashwin68732007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

CSP1150 Week2 Tutorial

The document outlines practice exercises for a programming principles course focusing on data types and selection in Python. It includes tasks for concept revision using the Python Shell, programming assignments for calculating BMI and grades, and a package discount program. Each question requires pseudocode, flowcharts, and Python code implementation.

Uploaded by

ashwin68732007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

CSP1150

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

• For each question Q2, Q3, and Q4,


a) Write a pseudocode
b) Draw a flowchart
c) Write a program in python
Q2 – BMI calculation Program (10 mins)
Write a program that calculates and displays a person’s body mass index (BMI). The BMI
is often used to determine whether a person is overweight or underweight for his or her
height. A person’s BMI is calculated with the following formula:
𝑤𝑒𝑖𝑔ℎ𝑡
𝐵𝑀𝐼 =
ℎ𝑒𝑖𝑔ℎ𝑡 2
where weight is measured in kilograms and height is measured in meters. The program
should ask the user to enter his or her weight and height, then display the user’s BMI.

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:

Quantity Discount for A Discount for B


10-19 10% 8%
20-49 20% 16%
50-99 30% 24%
100 or more 40% 32%

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy