Any non – zero
integer will return
the value of true,
MA1008 Introduction to Computational Thinking with 0 if it is false
Week 4 Tutorial: Program control – decisions and branching
This tutorial is on program and output control, in making decisions and branching.
1. What is printed in the follow statements
times = 5
print ("## "*times) ## ## ## ## ##
2. Assuming that x = 11, y = 6, z = 2, c = 'a', d = 'z', what are the values of the expressions in the
following table:
Value of expression
True
False
True
True
True
True
True
Type Error
True
‘e’ meaning True
3. The code below determines if a given number is small, medium, large or out of range. Correct the
errors that exist. After the correction, what is printed if you input 7?
Eg.
45*2 and "abc" => "abc"
5-5 and “abc” => 0
5*2 or “abc” => 10
5-5 or “abc”=> “abc”
4. What is the difference in the logic of the following two sets of code:
(i) if BooleanA: Ans:
Suite A
if BooleanB: In the first set, the checking of BooleanB and execution of Suite A are
Suite B independent of BooleanA, whereas in the second set, they are
(ii) if BooleanA: executed only upon BooleanA being false.
Suite A
elif BooleanB:
Suite B
5. Write a program that prints the largest of three numbers. a = int(input(‘a : ‘))
def find_largest(num1, num2, num3): b = int(input(‘b : ‘))
largest = max(num1, num2, num3) c = int(input(‘c : ‘))
print("The largest number is:", largest) if a >= and a>=c:
print(a, ‘is the biggest’)
# Taking input from the user
elif b>=c:
num1 = float(input("Enter the first number: "))
num2 = float(input("Enter the second number: ")) print(a, ‘is the biggest’)
num3 = float(input("Enter the third number: ")) else:
print(C, ‘is the biggest’)
# Calling the function to find the largest number
find_largest(num1, num2, num3)
6. You are writing a Python program to determine if an applicant qualifies for admission into NTU.
The selection uses two criteria: academic result and aptitude test. The academic result is a score in
the range 0-100 and the aptitude test is awarded one of these letter grades: A, B, C, D and E. A
candidate who satisfies one of the following two criteria qualifies:
• Academic score greater than or equal to 75 with aptitude grade of A, B, or C
• Academic score greater than or equal to 60 with aptitude grade of A only.
Your program stores the academic score in an integer variable called S and the aptitude grade in a
string variable called G. Write a Boolean expression in one line that returns True if an applicant.
qualifies based on the above criteria and False otherwise. (Do not write multiple statements.)
Ans: (S>=75 and G in ‘ABC’) or (S >=60 and G == ‘A’)
Note that G in ‘ABC’ maybe written as (G==’A’ or G==’B’ or G==’C’)