[go: up one dir, main page]

0% found this document useful (0 votes)
22 views39 pages

10 Nested If

The document outlines an online course provided by UNESCO UNITWIN and Handong Global University, focusing on practicing multiple conditional statements in programming. It includes learning objectives, exercises, and examples related to conditional logic, error identification, and practical applications. The course emphasizes the importance of understanding conditional statements and their proper syntax.

Uploaded by

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

10 Nested If

The document outlines an online course provided by UNESCO UNITWIN and Handong Global University, focusing on practicing multiple conditional statements in programming. It includes learning objectives, exercises, and examples related to conditional logic, error identification, and practical applications. The course emphasizes the importance of understanding conditional statements and their proper syntax.

Uploaded by

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

UNESCO UNITWIN

Online Course
Supported by

Handong Global University


Ministry of Education, Korea

* All copyrights belong to UNESCO UNITWIN and it is prohibited to use these educational materials including the video and other
relevant materials for commercial and for-profit use.
Nested If
10
Global Leadership School
Handong Global University
Learning Objectives
• Practice writing multiple conditionals
• Find mistakes in conditional statements
• Write expected output of the code
Multiple conditional statement
• List the conditions in one line

• Conditional within another conditional statement


• To determine the pass/fail of a test

• Practice through various exercises


Use multiple conditionals at once

• 0 < x < 10
• if x > 0 and x < 10 :
• If 0 < x < 10:

• 0 > x or y < 0
• if x < 0 or y < 0 :

• if a < 50 and b < 50 and c < 50 :


• if a >= 50 or b >= 50 or c >= 50 :
Use Boolean data type
a=1
b=2 # Boolean data type. True or False
c=3 a = True

# And if a :
if a < b and a < c : print("a is true")
print("a is less than b and c") else :
print("a is false")
# Non-exclusive or
if a < b or a < c :
print("a is less than either a or b (or both)")
Check month input
Birthdate = input(“Enter birthday (ex. 20150220): ")

Year = Birthdate[ 0 : 4 ]
Month = Birthdate[ 4 : 6 ]
Day = Birthdate[ 6 : 8 ]

if Month < “01” or Month > “12” :


print(“Incorrect month input. Try again.")

if Birthdate > “20220901” :


print(”Can not calculate age")

else:
age = 2022 - int(Year)

if Month > “09”:


age = age - 1

print(“You are“, age, “years old”)


Check Pass/Fail, or

Korean = int(input(”Enter Korean score: "))

English = int(input(”Enter English score: "))

Math = int(input(”Enter Math score: "))

History = int(input(”Enter History score: "))

if Korean < 50 or English < 50 or Math < 50 or History < 50 :


print(”Failed.")
Check Passed, and

Korean = int(input(”Enter Korean score: "))

English = int(input(”Enter English score: "))

Math = int(input(”Enter Math score: "))

History = int(input(”Enter History score: "))

Mean = (Korean + English + Math + History) / 4

if Korean >= 50 and English >= 50 and Math >= 50 and History >= 50 and Mean >= 60 :
print("Passed.")
Exercise 1, Find Mistake 1

#1
if num > 0 then
print(num)
Exercise 1, Answer 1

#1
if num > 0 :
print(num)
Exercise 1, Find Mistake 2

#2
if num > 0 :
print(num)
else
print(num + 10)
Exercise 1, Answer 2

#2
if num > 0 :
print(num)
else :
print(num + 10)
Exercise 1, Find Mistake 3

#3
if num = 10 :
num = 1
Exercise 01, Answer 3

#3
if num == 10 :
num = 1
Exercise 1, Find Mistake 4

#4
num = input(“Enter an integer : “)
s = num ** 2
Exercise 1, Answer 4

#4
num = input(“Enter an integer: “)
num = int(num)
s = num ** 2
Exercise 1, Find Mistake 5
#5
grade=“F”

if score >= 90 :
grade = “A”

if score >= 80 :
grade = “B”

if score >= 70 :
grade = “C”

if score >= 60 :
grade = “D”
Exercise 1, Answer 5

#5
grade=“F”

if score >= 90 :
grade = “A”
elif score >= 80 :
grade = “B”
elif score >= 70 :
grade = “C”
else :
grade = “D”
Exercise 2, Guess the output
#1
n=1
m = -1

if n < -m :
print(n)
else :
print(m)
Exercise 2, Answer
#1
n=1
m = -1

if n < -m :
print(n)
else :
print(m)
Exercise 2, Guess the output
#2
n=1
m = -1
l=0

if n <= -m and m < l:


print(n)
elif m >= l :
print(m)
else :
print(l)
Exercise 2, Answer
#2
n=1
m = -1
l=0

if n <= -m and m < l:


print(n)
elif m >= l :
print(m)
else :
print(l)
Exercise 2, Guess the output
#3
s1 = “ab”
s2 = “abc”
s3 = “bc”

if s1 < s2 :
print(“s1; “, s1)

if s1 < s3 :
print(“s1; “, s1)

if s2 < s3 :
print(“s2; “, s2)
Exercise 2, Answer
#3
s1 = “ab”
s2 = “abc”
s3 = “bc”

if s1 < s2 :
print(“s1; “, s1)

if s1 < s3 :
print(“s1; “, s1)

if s2 < s3 :
print(“s2; “, s2)
Exercise 3
• Receive Korean, English, and math score
• Calculate the average
• If at least one subject is below 50, it is
excessive fail
• If it is not excessive fail, and the average is
above 60, it is “pass”, if not, “failed”
Exercise 3 Code and result
Korean = int(input(”Korean score: "))
English = int(input(”English score: "))
Math = int(input(”Math score: "))

Mean = (Korean + English + Math) / 3

if Korean < 50 or English < 50 or Math < 50 :


print(”excessive fail")

elif Mean >= 60 :


print(”pass")

else :
print(”fail")
Exercise 4
• Receive integer 1,2,3, … ,12
• Assume that the input is the month and print
the last day of the month
• 1, 3, 5, 7, 8, 10, 12 => “31 days”
• February => “28 days or 29 days”
• 4, 6, 9, 11 => “30 days”
• If it's an integer other than that, "Input error".
Exercise 4, Code
mon = int(input(“Enter the month: "))

if mon==1 or mon==3 or mon==5 or mon==7 or mon==8 or mon==10 or mon==12 :


print(“31 days")

elif mon==2 :
print(“28 days or 29 days")

elif mon==4 or mon==6 or mon==9 or mon==11 :


print(“30 days")

else :
print(“Input error")
Exercise 4, Code With List
mon = int(input(”Enter the month : "))

if mon in [1,3,5,7,8,10,12] :
print(”31 days")

elif mon==2 :
print(”28 days or 29 days")

elif mon in [4,6,9,11] :


print(”30 days")

else :
print(”Input error")
Exercise 5
• Receive the season and the room temperature
• Season – Winter or Summer

• Check if indoor temperature is appropriate according to


the season
• Appropriate indoor temperature for winter is 20~22 degree
• Appropriate indoor temperature for summer is 24~26 degree

• Print received season and the indoor temperature


• If it is appropriate temperature, “It is appropriate temperature”
• If it is not appropriate temperature, “It is not appropriate
temperature”
Exercise 5, Code
season = input(”Enter the season: ")
temp = int(input(”Enter the indoor temperature: "))

If season==”winter":
if temp>=20 and temp<=22 :
print(”It is appropriate temperature")
else:
print(”It is not appropriate temperature")
elif season==”summer“ :
if temp>=24 and temp<=26 :
print(”it is appropriate temperature ")
else:
print(”It is not appropriate temperature")
else:
print(”You can only select winter or summer")
Lecture Summary
• Practice writing multiple conditionals
• Use two or more conditionals at once
• Use and, or, Boolean

• Find mistakes in conditional statements


• Understand conditional statements grammar
Practice Question 1
• Choose all errors in the following blocks.
num = 5
if num > 0
print(num)
else
print(num + 10)

• num = 5
• if num > 0
• print(num)
• else
Practice Question 1, Answer
• Choose all errors in the following blocks.
num = 5
if num > 0
print(num)
else
print(num + 10)

• num = 5
• if num > 0
• print(num)
• else
Practice Question 2
• What is the right explanation of the following
code
if mon in [1,3,5,7,8,10,12] :

• If the value of variable mon is one of


1,3,5,7,8,10,12
• Save 1,3,5,7,8,10,12 into variable mon
• Remove 1,3,5,7,8,10,12 from variable mon
• Variable mon is [1,3,5,7,8,10,12]
Practice Question 2, Answer
• What is the right explanation of the following
code
if mon in [1,3,5,7,8,10,12] :

• If the value of variable mon is one of


1,3,5,7,8,10,12
• Save 1,3,5,7,8,10,12 into variable mon
• Remove 1,3,5,7,8,10,12 from variable mon
• Variable mon is [1,3,5,7,8,10,12]
Thank you
10 Nested If
Contact Info
unitwinlms@handong.edu
https://ecampus.handong.edu/

* All copyrights belong to UNESCO UNITWIN and it is prohibited to use these educational
materials including the video and other relevant materials for commercial and for-profit use.
CREDITS: This presentation template was created by Slidesgo, including icons by Flaticon, and infographics & images by Freepik.

You might also like