MA1008 Week 4 (Program Control - Decisions and Branching)
MA1008 Week 4 (Program Control - Decisions and Branching)
MA1008
Program Control
Decisions and Branching
Week 4
Week 4_Tutorial_Question 1
What is printed in the following statements?
times = 5
print ("## "*times)
Ans: ## ## ## ## ##
For parts i and j, remember that non-empty strings, lists, tuples and dictionaries are considered “True”.
Week 4_Tutorial_Question 3
The code below determines if a given number is small, medium, large or out of range. Correct
any error that might exist. After the correction, what is printed if you input 7?
num1 = input ("Enter a positive integer: ")
Hint:
if num1< 5 A quick examination of this
if num1 <= 0 code suggests that any
positive number between 0
print num1 ("is out of range") (excluding zero) and 5
(excluding 5) is a small value.
else
print num1; (" is a small value.") Any positive number above 5
is a large value.
elseif num1 = 5
print num1; ("is a medium value. ") 5 is a medium value.
else #num1 > 5
print num1; ("This is a large value. ")
Week 4_Tutorial_Question 3
The code below determines if a given number is small, medium, large or out of range. Correct
any error that might exist. After the correction, what is printed if you input 7?
num1 = int( input ("Enter a positive integer: "))
if num1< 5:
if num1 <= 0:
print (num1, "is out of range")
else:
print ( num1, " is a small value.")
elif num1 == 5:
print ( num1, "is a medium value. ")
else: #num1 > 5
print ( num1, "This is a large value. ")
Week 4_Tutorial_Question 4
What is the difference in the logic of the following two sets of code:
(i) if BooleanA:
Suite A
if BooleanB:
Suite B
(ii) if BooleanA:
Suite A
elif BooleanB:
Suite B
In the first set, the checking of BooleanB and execution of Suite B are independent of BooleanA, whereas in the
second set, they are executed only upon BooleanA being False.
Week 4_Tutorial_Question 5
Write a program that prints the largest of three numbers.
This is a repeat of a question in the previous tutorial. But now the students have to deliver the solution
in Python. Assume that the three numbers are stored in variables A, B and C. (Students should use the
input() function to read in the three variables.)
In this exercise, you are to devise Python programs to solve simple problems. But before you
start the coding on each problem, you are strongly advised to think about the followings:
After that, you can first build a simple skeleton for each major steps using simple English. That
is, write the pseudo-code.
Week 4_Hands-On_Question 1
Write a Python program that requests the number of hours one worked in a month and then
prints out the gross pay, taxes, and net pay. Assume that the pay structure and tax rate are as
follows:
• Basic pay rate = $10.00 per hour
• Overtime (>160 hours) = one and a half time of the basic pay rate
• Tax rate = 10% for first $1000, 20% for next $500, and 30% for the rest
Week 4_Hands-On_Question 1
basic_pay = 10.0
hours = float(input("Enter hours worked: "))
# Calculate pay
if (hours <= 160):
pay = hours*basic_pay
else:
pay = 160*basic_pay + (hours-160)*basic_pay*1.5
# Calculate tax
if (pay <= 1000):
tax = pay*0.1
elif (pay <= 1500): # 100 is tax for first $1000
tax = 100 + (pay-1000)*0.2
else: # pay > 1500, 200 is tax for first 1500.
tax = 200 + (pay-1500)*0.3
# Get inputs
print("please input birthday of 1st person" )
y1 = int( input( " year: " ) )
m1 = int( input( " month: " ) )
d1 = int( input( " day: " ) )
print("please input birthday of 2nd person" )
y2 = int( input( " year: " ) )
m2 = int( input( " month: " ) )
d2 = int( input( " day: " ) )
Week 4_Hands-On_Question 3
# compare the integers
if y1 < y2:
print( "person 1 is older" )
elif y2 < y1:
print( "person 2 is older" )
else: #the program flows into this portion only if y1 is the same as y2
if m1 < m2:
print( "person 1 is older" )
elif m2 < m1:
print( "person 2 is older" )
else: #the program flows into this portion only if m1 is the same as m2
if d1 < d2:
print( "person 1 is older" )
elif d2 < d1:
print( "person 2 is older" )
else:
print( "they have the same birthday" )
Week 4_Hands-On_Question 4
A year is a leap year if it is divisible by 4 and not by 100, or if it is divisible by 400. Otherwise, it
is not a leap year. Write a program that reads in an integer value and returns true if the value
represents a leap year and false otherwise.
if (day == 1):
print("Monday")
elif (day == 2):
print("Tuesday")
elif (day == 3):
print("Wednesday")
elif (day == 4):
print("Thursday")
elif (day == 5):
print("Friday")
elif (day == 6):
print("Saturday")
elif (day == 7):
print("Sunday")
else:
print("Illegal input")
Week 4_Hands-On_Question 6
Write a program that reads in the coefficients of a quadratic equation and prints the roots
when real roots exist and the message “No real roots” otherwise.
Hints
We begin addressing this question by thinking about the following:
1) Asking the user to input the coefficients of a quadratic equation: 𝐴𝑥2 + 𝐵𝑥 + 𝐶 = 0
2) solving for the discriminant of a quadratic equation: B2 − 4AC
We know that when the discriminant is positive, there are 2 real roots, when discriminant = 0, there is 1
root. For now, let us ignore the case of a negative discriminant.
The trick is to use a very small number that is close to zero, that ‘catches’ all these small inaccuracies so that
very small values of the discriminant are essentially regarded as zero.
This question also exposes us to the use of importing the ‘math’ library for square roots.
Week 4_Hands-On_Question 6
Write a program that reads in the coefficients of a quadratic equation and prints the roots
when real roots exist and the message “No real roots” otherwise.
import math
Hints
Again like Question 6, this question deals with the use of epsilon (eps) as a very tiny number to
represent zero when comparing floating-point numbers when programming.
See the following page for a graphical representation of what Question 7 describes.
Week 4_Hands-On_Question 7
𝑦
Quadrant 2 Quadrant 1
Dotted line
Dotted line
with a value of Dotted line
with a value of
-eps on 𝑥 axis with a value of
+eps on 𝑥 axis
+eps on 𝑦 axis
Dotted line 𝑥
with a value of
-eps on 𝑦 axis
Quadrant 3 Quadrant 4
Week 4_Hands-On_Question 7
# Get inputs # which quadrant?
x = float( input( "input x: " ) ) else:
y = float( input( "input y: " ) ) if x > 0:
if y > 0:
#define the value of eps as 1x10-7 print( "in quadrant 1" )
eps = 1e-7 else:
print( "in quadrant 4" )
# On axis or origin? else:
if abs( x ) < eps: if y > 0:
if abs( y ) < eps: print( "in quadrant 2" )
print( "at origin" ) else:
else: print( "in quadrant 3" )
print( "on y-axis" )
elif abs( y ) < eps:
print( "on x-axis" )