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

Conditional S-1, ICT Cource

Conditional S-1

Uploaded by

amirkhanovcr
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)
12 views60 pages

Conditional S-1, ICT Cource

Conditional S-1

Uploaded by

amirkhanovcr
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/ 60

Unit learning objective (1/3) UNIT 06

Unit learning objective (2/3) UNIT 06


Unit learning objective (3/3) UNIT 06

Sequential Conditional if
Statement Statement Statement

Boolean Conditional
Data Type Expression
UNIT 06

1.1. What is lottery?


UNIT 06

1.2 Lottery simulation with 3 integers


UNIT 06

1.3. History of lottery


UNIT 06

1.4. Various lottery systems


UNIT 06

1.5. Lottery number recommendation system website

1 2 Select country
Access the website
https://www.random
.org/quick-pick/

The site that predicts


lottery numbers.
UNIT 06

1.5. Lottery number recommendation system website

4 Shows any number recommended by this system.


3 Select type of the lottery
UNIT 06

2.1. How the lottery machine works

('Enter a three-digit lottery number : ').split()

# If one of the numbers match the winning number


#Increase n

# If n is 3, three numbers matched


('You won the lottery!')

Enter a three-digit lottery number :


You won the lottery!
UNIT 06

2.2. Mechanism of automatic lottery machine

('Enter a three-digit lottery number : ').split()

# If one of the numbers match the winning number


#Increase n

# If n is 3, three numbers matched


('You won the lottery!')

Enter a three-digit lottery number : 1, 2, 9


UNIT 06

2.3. Programming Plan


Start

[1] Start initialize n1=2, n2=3, n3=9

[2] Generate three numbers, 2, 3, 9. a, b, c = input()

[3] Reset with n = 0. if a == n1 or a == n2 or


a == n3:
[4] Receive input for three numbers a, b, c.
n += 1
[5] if a matches one of 2, 3, 9, then {
if b == n1 or b == n2 or
[6] Add 1 to n } b == n3:

[4] if b matches one of 2, 3, 9 then { n += 1

[8] Add 1 to n } if c == n1 or c == n2 or
c == n3:
[9] if c matches one of 2, 3, 9 then {
n += 1
[10] Add 1 to n }
if n == 3
[11] if n is 3, print ‘You won the lottery!’
print ‘You won the lottery’
[12] End
End
UNIT 06

2.4. Automatic lottery machine final code

('Enter a three-digit lottery number : ').split()

# If one of the numbers match the winning number


#Increase n

# If n is 3, three numbers matched


('You won the lottery!')
UNIT 06

1.1 Conditional statements that have different flows depending on the


conditions
UNIT 06

1.2. Basic control structures of a program

‣ Sequence - a structure in which commands are executed sequentially.


‣ Selection - a structure in which one of several instructions is selected and executed.
‣ Iteration - a structure in which the same command is executed repeatedly.

순차구조
Sequence 선택구조
Selection 반복구조
Iteration

거짓
False 거짓
False


True 참
True
UNIT 06

1.2. Basic control structures

‣ The Sequence can be considered as a road through


which a car goes straight as shown in Figure 1.
‣ The Selection is an intersection where a car selects
one of the two roads and drives, as shown in Figure 2.
‣ The iteration as shown in Figure 3, can be said to be a
roundabout where the vehicle rotates and travels.
This structure can iterate a statement multiple
times.

<Figure 1> < Figure 2> < Figure 3>


UNIT 06

1.3. Sequential statements

# 100 is printed
# 100 is added to num and 200 is printed
# 100 is added again to num and 300 is printed

100
num = 100 num + 100

200
num = num + 100 num + 100

300
num = num + 100 num

A structure in which commands are executed sequentially


UNIT 06

1.4. Sequential statements and various flow statements

‣ Conditional statement : if statement, if-else statement, if-elif-else statement


‣ Loop statement : for statement, while statement
‣ Commands that change the flow of a loop statement : break, continue
UNIT 06

1.5. Why we need a selection structure

‣ At some stages of the program, there are more than one paths to proceed, among which we have to select one.
‣ If there is no selection structure, the program will always repeat the same action. If the program always does the
same thing, it will always reach a fixed conclusion.
UNIT 06

1.6. Flow of conditional statement

How can we choose to run only one of several executable statements? Let's express this in a flowchart.

A figure showing such flow of


tasks is called a branch.
True False
Conditional
Statement

Executable Executable
statement 1 statement 2

• <Executable Statement 1> or <Executable Statement 2> is executed according to specific conditions.
• Conditions are determined by <Conditional Statement>. The conditional statement must have a True or False value.
UNIT 06

1.7. If statement and related terms that are executed in specific circumstances

Situation 1: print "youth discount" if the age is under 20.


Situation 2: print "goal achieved" if step count over 1,000.

‣ Implementing this code requires an expression that determines whether the condition is satisfied or not.
‣ This is called a conditional expression. And this conditional expression is evaluated in Boolean type, which contains
True or False values.
‣ The relational operator from the previous chapter is used to compare two operands, and the result of the relational
operator is shown in True or False value. Thus, it can be used in conditional expressions.
UNIT 06

1.8. if statement syntax

if keyword Conditional statement colon

if ` :

indent block executed when a condition is true

Situation 1 : Situation 2 : ‣ (Situation 1) - In the conditional sentence that appears


print "youth discount" if print "goal achieved" if before the colon (:), print ('youth discount') is executed
the age is under 20 step count(walk_count) is
only when the age is less than 20 years old using
over 1,000.
< operator.
‣ (Situation 2) - In the conditional sentence, use the >=
if age < 20 : if walk_count >= 1000 : operator to execute a code, print ('goal achievement'),
print(‘youth discount') print(‘goal achieved') when the walk_count reaches 1,000 or more.
UNIT 06

1.9. if statement example code

# if age is 18
# result of age < 20 is True
('youth discount')
youth discount

‣ When the age value is 18, the conditional statement of age <20 becomes True,
so 'youth discount' is printed on the screen.
‣ When the age value is 20 or more, no output is printed.
False
age < 20

True

print(‘youth discount’)
UNIT 06

1.9. if statement example code


Example of converting to a different flow according to the value of the age variable

18 24

age age

False False
age < 20 age < 20

True True
print(‘youth print(‘youth
discount’) discount’)

# if age is 18 # if age is 24
# result of age < 20 is True # result of age < 20 is False
('youth discount') ('youth discount')
youth discount
UNIT 06

1.10. Block of if statement


Conditional statement and block

‣ Learn the necessity of indentation and the term block.


‣ The code below prints an error.

('youth discount')

('youth discount')

‣ A chunk of code that can be executed when a condition is true is called a block.
‣ The conditional block of a conditional statement must be indented. Otherwise, an error occurs as above.
UNIT 06

1.11. Indentation
Focus Python is a programming language in which indentation is significant. Depending on the indentation, the
same code produces different results. Let's take a closer look at the output from indented codes from 1 to 5.

‣ [Indentation code 1] If the conditions of age <20 is satisfied

('youth discount')
('Welcome')
youth discount
Welcome
‣ [Indentation code 2] If the conditions of age <20 is not satisfied

('youth discount')
('Welcome')
Welcome
UNIT 06

1.11. Indentation
‣ [Indentation code 3] If the condition ‘age <20’ is satisfied

('age', age)
('youth welcome')
('youth discount')

Age 18
youth welcome
youth discount

Line 3-5
• Since age is 18, age <20 is True and Line 3-5 are printed.
UNIT 06

1.11. Indentation
‣ [Indentation code 4] If the condition age <20 is not satisfied
24
('age', age)
('youth welcome')
('youth discount')

No output

Line 3-5
• Since the age is 24, age <20 is False and Line 3-5 are not printed.
UNIT 06

Wrong Indentation

('age', age)
('youth welcome')
('youth discount')

('youth welcome')
UNIT 06

('youth discount')

youth discount
t

('youth discount')
youth discount
UNIT 06

2.1. Code to verify if a number is a multiple of 3

('Enter an integer: ')) # Change input value to integer type

'is a multiple of 3.')


Enter an integer : 6
6 is a multiple of 3.

Line 2
• If the number is divided by 3 and the remainder is all 0, line 3 is executed (If the value of module 3 is 0, it's a
multiple of 3)
• In this way, it is possible to determine whether the number is a multiple of 3.
UNIT 06

2.2. Code to verify if a number is a multiple of 3 and 5

('Enter an integer: '))

'is a multiple of 3 and 5.')

Enter an integer : 15
15 is a multiple of 3 and 5.

Line 2, 5
• If the remainder is zero when the number is divided by 3, and the remainder is zero when divided by 5, line 3 is
executed.
• In this way, it is possible to determine whether the number is a multiple of 3 as well as 5.
UNIT 06

2.3. == operator

('ID matches.')

Enter your id: david


ID matches.

Line 2, 5
• If the string input is 'david', the two strings s and my_id match, and Line 5 is executed. When another string is
given, Line 5 is not executed.
UNIT 06

3.1. Even-number discrimination scenario

("Enter an integer : "))

"is an even number.")

Enter an integer : 50
n = 50
50 is an even number.

Line 3
• Checks if the remainder is 0 when the input n is divided by 2. If the input value is 50, the equation is True, so
line 4 is executed.
UNIT 06

3.2. Scenario verifying if two strings match

('Two strings match.')

('Two strings are different.')

Two strings are different.

Line 3
• Runs Line 4 block if the two strings are identical. In this case, it is not printed since the values of str1 and str2
are different.
UNIT 06

3.2. Scenario verifying if two strings match

('Two strings match.')

('Two strings are different.')

Two strings are different.

Line 5
• If the values of the two strings are different, Line 6 block is executed. In this case, it is printed because the
values of str1 and str2 are different.
UNIT 06

4.1. Role of pass statement

('Even number')

Even number

Line 5
• It leaves a block empty to complete the functionality later.
UNIT 06

4.1. Role of pass statement

Line 4
• func function does not do anything because there is no code inside the func() function.
UNIT 06

4.2. pass statement’s role


pass statement plays an important role.

‣ Although it does not have a functionality, the pass statement plays a role as a placeholder.
‣ Therefore, if the pass statement is omitted inside the function in the above sentence, an error occurs.
UNIT 06

def func_name(x1, x2, ... ) : t def func_name(x1, x2, ... ) :


code1 pass
code2
… A function with no body constructed
return n1[, n2,…]

The general form of a function


Try to fully understand the basic concept before moving on to the next step.
Lack of understanding basic concepts will increase your burden in learning this
course, which may make you fail the course.
It may be difficult now, but for successful completion of this course we suggest you
to fully understand the concept and move on to the next step.
UNIT 06

Enter game score : 1500


game_score = 1500
You are a master.
Example Output or
Enter game score : 100
game_score = 100

Time

Write the entire code and the expected output results in the note.
UNIT 06

Enter integer:
x = 50
50 is a natural number.
Example Output
or
Enter integer:
x = -10

Time

Write the entire code and the expected output results in the note.
UNIT 06

1.1. If statements that are executed when certain conditions are satisfied

("x is greater than 1.")

x is greater than 1.

‣ Check the function of the if statement with an actual code. Put 100 in the variable x and write the if statement as
above. In this case, since the conditional expression x>1 returns True, the following execution is run.
‣ Therefore, 'x is greater than 1' is printed on the screen. Here, the line below :(colon) must be indented.
UNIT 06

1.2. Relational operator =, ==


Focus Assignment operator = and relational operator == are different operators.

‣ This part is mentioned repeatedly, because beginners often make these mistakes. The = operator is an operation that
puts the value of right to the variable on the left. The == operator returns True or False depending on whether the
left and right values are identical or not.
UNIT 06

1.3. Indentation in Python

‣ How can we print that one is accepted and qualified for a scholarship if the score is 90 or higher? In this case,
sentences can be grouped by using indentations as shown below.
‣ If the value of score is 90 or higher, two sentences calling print() are executed in the code below. Note that these
sentences have the same number of spaces. All of these belong to the same block.

Block: a group of sentences.

if score >= 90 :
print(“You are accepted.”)
print(“You are qualified for a scholarship.”)
UNIT 06

1.3. Indentation in Python

("Enter the score: "))


# if the condition is true, block indent is all executed
("Congratulations.")
("You are accepted.")
("You are qualified for a scholarship.")

Enter the score: 95


Congratulations.
You are accepted.
You are qualified for a scholarship.

Line 2-5
• Line 3, 4 and 5 are executed because the score is given as 95.
UNIT 06

1.3. Indentation in Python

IndentationError

("Enter the score: "))


# Caution : if indentation is different an error occurs
("Congratulations.")
("You are accepted.")
("You are qualified for a scholarship.")

("You are accepted.")

Line 3-5
• Error occurs because the numbers of indented spaces are different.
UNIT 06

2.1. split method

The split uses space as the default value. So, each value is inserted using the space as a separator.
In this way, a character that divides a string into one or more individual strings is called a separator.
UNIT 06

2.2. input function and split method

('Enter three integers divided by space: ')


('Input: ', str1)
('Input separated by split() method: ', str1.split())

Enter three integers separated by space: 10 20 30 separated by spaces


Input: 10 20 30
Input separated by split() method: ['10', '20', '30']

('Enter three integers separated by ,: ')


('Input: ', str1)
('Input separated by split() method: ', str1.split(','))

Enter three integers separated by ,: 10 20 30 separated by commas


Input: 10,20,30
Input separated by split() method: ['10', '20', '30']

‣ Make a list of string using string received by split function.


‣ After that, the elements in this list can be converted to an integer type.
UNIT 06

2.3. How to process input values with split and int functions

('Enter three integers separated by ,: ').split(',')

Enter three integers separated by ,:

Enter three integers separated by ,:

('Enter three integers separated by ,: ').split(',')

Enter three integers separated by ,: 5,6,7


5 6 7
UNIT 06
UNIT 06
UNIT 06

Output example
Enter age: 16
Youth
Enter age: 33
Adult
Enter age: 5
Kid
UNIT 06

Output example
Enter age: 20
Enter height in cm: 180
You can enter.

You might also like