CLO3 - Part 2 (Week 07)
CLO3 - Part 2 (Week 07)
2
LECTURE NOTES
3
1. NESTED IF STATEMENT
KEY TERMS
KAHOOT!QUIZ
4
LECTURE OBJECTIVES
5
1. NESTED IF STATEMENTS
6
1. NESTED IF STATEMENT – EXAMPLE
Write a Python program to ask users to enter a number. The program will
determine and display a given number which is either less than 1, a single digit
number (1 – 9) or a positive number that is more than one digit.
Sample Outputs
7
2. NESTED ELSE IF STATEMENT
There are situations when you need to apply another condition in case
the answer for the first condition is false. In this case we replace else
with elif.
8
2. NESTED ELSE IF STATEMENT- EXAMPLE
Write a Python
program to ask
users to enter an
age. The program
determines and
display a message
as shown in bellow
table.
age Message Sample
outputs
Upto 12 Child
13 to 19 Teenager
20 to 59 Adult
9
60 and
above Senior
3. NESTED IF WITH NESTED ELSE IF STATEMENT
There are situations when you need to apply both nested if with
nested else if.
10
3. NESTED IF WITH NESTED ELSE IF STATEMENT – EXAMPLE
1
Write a Python program to ask users to enter three numbers. The
program determines and displays the biggest number entered.
Sample outputs
11
3. NESTED IF WITH NESTED ELSE IF STATEMENT – EXAMPLE
2
Grade
Write a Python Grade Range Points
A 90 – 100 4
program to ask users
A- 87 – 89 3.7
to enter a mark out of
B+ 84 – 86 3.3
100. The program
B 80 – 83 3
finds the equivalent B- 77 – 79 2.7
letter grade using the C+ 74 – 76 2.3
grading system. C 70 – 73 2
C- 67 - 69 1.7
D+ 64 - 66 1.3 12
D 60 – 63 1
3. NESTED IF WITH NESTED ELSE IF STATEMENT – EXAMPLE 2
(CONTD.)
SOLUTION
13
4. BEST CODING PRACTICES – CODE LAYOUT
BLANK LINES - How the layout your code plays a huge role in its readability
Vertical whitespace, or blank lines, can greatly improve the readability of your code. Code that’s bunched up together can
be hard to read. Similarly, too many blank lines in your code makes it look very sparse, and the reader might need to
scroll more than necessary.
Use blank lines sparingly to show clear steps
+ arg_three \
+ arg_four
4. BEST CODING PRACTICES – CODE LAYOUT
INDENTATION:
An indentation or indent is an empty space at the beginning of a line to signal the
start of a new paragraph. However, you should be careful with it, as it can lead to syntax
errors.
An indent is usually used to define the block of code such as condition, loop, function.
The indentation level of lines of code in Python determines how statements are grouped
together.
n = 20
if(n > 0):
if(n < 10):
print("Single Digit")
else :
print("More than one
Digit") 16
else:
print("Negative number")
5. EXERCISES –(CONTD.)
Exercise 2: Modify the above example to display the Grade Point Average
(GPA) based on the mark.
Exercise 3: Modify the above program to allow the student to enter the marks
for 5 courses and calculate the semester average and GPA.
17
5. EXERCISES –(CONTD.)
Exercise 5: Write a python program to accept two numbers as input and find
out which is the larger number. If both are equal then print the result as “Both
are Equal”.
18
NESTED IF
ELIF
EXAMPLE
PROCESS
19
FORMATIVE KAHOOT! Q
ASSESSMENT UIZ1
20
21