Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

PRG1102 Programming Principles

Lab 2 :

Concepts cover – Selection Structure, Repetition Structure, Condition Statement

Objective of this lab Exercise:

1. Let student familiarize with the single selection ‘if’ statement.


2. Let student familiarize with the usage of the ‘if..else’, ‘if-elif-else’ multiple selection
statement
3. Let student familiarize with the usage of the for loop and while loop

Notes to instructor

1. Explain the usage of ‘if’ , ‘if..else’, ‘for’, ‘while’ statement

Exercise 1 Please do it in exercise1.py. Write a program to read in the student’s mark and print out
the grade.

Solution 1

mark = int(input("Please enter your mark:"))

while mark > 100 or mark <0:

print("invalid number")

mark = int(input("Please enter your mark:"))

if mark >= 70 and mark < 100:

print("Grade A")

elif mark >= 60 and mark < 70:

print("Grade B")

elif mark >= 50 and mark < 60:

print("Grade C")
elif mark >= 40 and mark < 50:

print("Grade D")

elif mark >= 0 and mark < 40:

print("Grade F")

Solution 2

token = False

grade= int(input("Please enter a grade value:"))

if grade >= 0 and grade <= 100:

if grade >= 70:

print("A")

elif grade >=60:

print("B")

elif grade >=50:

print("C")

elif grade >=40:

print("D")

else:

print("F")

else:

print("Invalid grade")

Solution 3

token = False

grade = int(input("Please enter a grade value:"))


while token is False:

if grade < 0 or grade > 100:

print("Value is incorrect/invalid")

grade = int(input("Please enter a grade value:"))

else:

token = True

if grade >= 70:

print("A")

elif grade >= 60:

print("B")

elif grade >= 50:

print("C")

elif grade >= 40:

print("D")

else:

print("F")

Exercise 2 Please do it in exercise2.py. The simple interest on a loan is calculated by the formula:

Interest = principal * annualrate * days / 365;

Develop a program that will ask user to input the principal, annualrate and days for several loans
then calculate and display the interest for each loan. The program should end only when user input
‘-1’. Use the sentinel-controlled loop in this exercise. Sample output as below:
Note:
Your program should be able to cater for inputs of different values, hence DO NOT HARDCODE the
values!

principal = int(input("Please enter the value of principal: (-1 to end): "))

while principal != -1:

annualrate = float(input("Please enter the value of the annualrate:"))

days = int(input("Please enter the number of days:"))

Interest = principal*annualrate* days/365

print(f"The Interest charge is ${Interest:.2f}:")

principal = int(input("Please enter the value of principal: (-1 to end)"))

Exercise 3 Please do it in exercise3.py. Write a program that utilizes looping to produce the following
table of values. Use the counter-controlled loop in this exercise.
#counter control loop

print("A\t\tA+2\t\tA+4\t\tA+6")

for x in range(3,16,3):

print(f"{x}\t\t{x+2}\t\t{x+4}\t\t{x+6}")

print("\n--------------\n")

# while loop method

print("A\t\tA+2\t\tA+4\t\tA+6\n")

x=3

while x <= 15:

print(f"{x}\t\t{x+2}\t\t{x+4}\t\t{x+6}")

x+=3

Tips:

Make use of print and f-string for formatting.

Lab Assignment #1 10%


This exercise is the lab assignment required to submit. Please do it in submission.py and follow the
previous submission step.

Write a program that inputs one six-digit number, separates the number into its individual digits and
prints the digits separated from one another by 3 spaces each print them in reverse order. After
that, please print out the sum of first and last digit.

Requirement:

 Use combinations of integer division(//) and the remainder operation (%) to retrieve each
digit from the number.
For example, if the user types in 246803, the program should print

Note:
Your program should be able to cater for inputs of different values, hence DO NOT HARDCODE the
values! Your program must follow the following exact format in sample output or risk do not grade
successfully because your script will be graded by the system automatically.

# Get input from user

value = int(input("Enter a six-digit number: "))

# Retrieve each digit from the number

huixiang6 = value % 10

huixiang5 = (value // 10) % 10

huixiang4 = (value // 100) % 10

huixiang3 = (value // 1000) % 10

huixiang2 = (value // 10000) % 10

huixiang1 = value // 100000

# Print digits in reverse order with 3 spaces between them

print(huixiang6, end=" ")

print(huixiang5, end=" ")

print(huixiang4, end=" ")

print(huixiang3, end=" ")

print(huixiang2, end=" ")

print(huixiang1)

# Calculate and print sum of first and last digit

value = huixiang1 + huixiang6

print("Sum of first and last digits:", value)


Take home exercise.
Question 1: Formulas for calculating BMI are

Create a BMI calculator application that reads the user’s weight in kilograms and height in meters,
then calculates and displays the user’s body mass index. Also, the application should display the
following information from the Department of health and Human Services/National Institutes of
Health so the user can evaluate his/her BMI. BMI Values if less than 18.5, display ‘Underweight’,
otherwise display ‘Normal’

Question 2:
Company X is introducing a new data plan for smartphones. Each GB of data
will cost RM 15, up to 10 GB. Any data over 10GB will be charged at RM 30
per GB. Write a program that prompts the user to enter their monthly data usage
(in GB), and prints the data charges for the month.

Question 3:

Write a program that prompts the user for his/her yearly income, and outputs
the amount of taxes to pay based on the yearly income. The tax table is as
follows:
a. RM 0 to 2,500 – Tax rate: 0%
b. RM 2,501 to 10,000 – Tax rate: 5%
c. RM 10,001 to 50,000 – Tax rate: 15%
d. Exceeding 50,001 – Tax rate: 25%
For example, if the income is RM 12,000, the total taxes would be RM 1,800
(15%).

You might also like