Page 46 - Code & Click - 7
P. 46
Experiential Learning
Lab Activity 1
Program to accept a number from the user and display if the number is positive.
Start
1. Type the code as shown in the image in the Python Editor. Is
Input Number Test Condi on 1 No
N True?
Is No Yes
N > 0 ?
Yes No Is Execute
Test Condi on 1 else block
Print N is Print N is True?
Posi ve Nega ve
2. Press the F5 key to execute the code. Sample output of the program is given below. Yes
Stop
Execute
if block
Is
Test Condi on Yes Execute
True? if block Is
Test Condi on 1 Yes Execute
True? if block 3
No
No
IF… ELSE STATEMENT
Is
The if … else statement is used to evaluate a test condition. If the test condition evaluates to True, the Execute Yes Test Condi on 2 Yes Execute
block of statements under if is executed. If the test condition evaluates to False, the block of statements if block 3 True? if block 3
under else is executed. After the execution of either of the two blocks of
statements, the control passes to the statement after the else block. No
Is
The syntax for using the if ... else statement is: Test Condi on Yes Execute Is
if block
if <test condition>: True? Test Condi on 3
True?
set of statements under if No
else: No
Execute
set of statements under else else block Execute
other statements else block
Experiential Learning
Lab Activity 2
Program to input age of user and display whether the user is eligible to vote or not. 64
1. Type the code as shown in the image in the Python Editor.
2. Press the F5 key to execute the code. Sample output of the program is given.
44