0% found this document useful (0 votes)
12 views21 pages

CLO3 - Part 2 (Week 07)

The document provides lecture notes on nested selections in programming, focusing on nested if and else if statements in Python. It outlines coding practices, including code layout, indentation, and line length, to enhance readability. Additionally, it includes exercises for students to apply their understanding of these concepts in practical scenarios.

Uploaded by

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

CLO3 - Part 2 (Week 07)

The document provides lecture notes on nested selections in programming, focusing on nested if and else if statements in Python. It outlines coding practices, including code layout, indentation, and line length, to enhance readability. Additionally, it includes exercises for students to apply their understanding of these concepts in practical scenarios.

Uploaded by

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

ICT 2013- COMPUTATIONAL

THINKING AND CODING


CHAPTER ???
NESTED SELECTIONS

CHAPTER ??? CLO3: Develop computer


algorithms to solve real life
programming problems
Versio Author​ Effecti Change De DRC DOCUMENT
n​ ve Dat
e​
scription​ No​
REVISION
1.0​ Anand
Pandiyan
Sept
2020​
Define the
first version​
001​ CONTROL
(DRC)
1.1​ Dr Sept Major 001​
Madelein 2021 changes: ad
e Togher ding
few example
s​and the
code layout
slides
Z: MAJOR CHANGE

2
LECTURE NOTES

 Contents of lectures are based on the textbook and supplementary material


 Please read Supplementary material from page
 49 to 51

 Please read text book from page


 94 to 95

3
1. NESTED IF STATEMENT

2. NESTED ELSE IF STATEMENT


3. NESTED IF WITH NESTED ELSE IF
STATEMENT
4. BEST CODING PRACTICES – CODE LECTURE
LAYOUT OUTLINE
4. EXERCISES

KEY TERMS

KAHOOT!QUIZ

4
LECTURE OBJECTIVES

 At the end of lecture, the student should be able to:

 Formulate the required logic to build a nested if statement to solve many


computational problems.
 Use best coding practices by understanding code layout in Python.

5
1. NESTED IF STATEMENTS

There are other situations when you need to apply an additional


condition just in case the first condition was True. In this case
you need to use another normal if instead of a process.

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

length = 100 length = 100


width = 50 width = 50
area = length*width
print("Area of box ",area) area = length*width

print("Area of box ",area)


Not Recommend
Recommend: separate assignment, process
14
and output using use blank lines
4. BEST CODING PRACTICES – CODE LAYOUT
MAX LINE LENGTH AND LINE BREAKS
PEP 8 suggests lines should be limited to 79 characters. This is because it allows you to have
multiple files open next to one another, while also avoiding line wrapping. When its not always
possible PEP 8 recommends ways to allow statements to run over several lines using line breaks.
Python will assume line continuation if code is contained within parentheses, brackets, or braces
If it is impossible to use implied continuation, then you can use backslashes (\) to break lines instead:
If line breaking needs to occur around binary operators, like + and *, it should occur before the
operator.
arg_one = 10
arg_two = 20
arg_three = 25
arg_four = 30

total = arg_one + arg_two \ 15

+ 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 1: Convert the previous marks-to-grade example to read the grade


letter and display the associated mark range.

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 4: Write a python program to accept the input of a number and


check whether the number is positive, negative or zero.

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

CONDITION KEY TERMS

EXAMPLE

PROCESS
19
FORMATIVE KAHOOT! Q
ASSESSMENT UIZ1

20
21

You might also like