Python Chapter 5.3-5.4
Python Chapter 5.3-5.4
Python Chapter 5.3-5.4
▪ Initialize TEST
LCV
▪ Loop test
▪ Updating STATEMENTS
TO REPEAT
STATEMENTS
UPDATE
LCV
for i in range(1,10):
print(i)
i=1
i<10
print(i)
i=i+1
Initialize LCV
while condition_test_LCV:
statements_to_repeat
Update LCV
Statements
INITIALIZE
LCV
True
statements
event
condition
True
False
break
statements
The following loop executes 50 times. For each Questions
execution, it displays the value of its loop control
variable (lcv).
▪ How many times the loop executes? Explain
lcv=1
your answer!
while lcv <=50:
print("value of lcv = ",lcv)
lcv=lcv + 1 ▪ How many times does the following statement
be executed?
print("loop stop at lcv value ",lcv)
print("loop stop at lcv value ",lcv)
The loop is then modified by adding an
QUESTIONS
event
▪ How many times the loop should
lcv=1 executes?
while lcv <=50:
If lcv==47:
break ▪ How many time the loop executes?
print("value of lcv = ",lcv)
lcv=lcv + 1
▪ How many times statement lcv=lcv+1
print("loop stop at lcv value ",lcv) executes?
True
statements
event1 True
condition
False break
True
event2
condition
continue
False
statements
▪ Observe the output produced by the following ▪ QUESTIONS
codes ▪ What happen when letter ‘h’ is
encountered in the loop?
for letter in 'Python':
if letter == 'h':
continue
print ('Current Letter :', letter)
▪ Observe the output produced by the QUESTIONS
following codes: ▪ What values omitted by the loop?
Explain your answer.
var = 10
while var > 0:
var = var -1
if var%3 == 0:
continue
print ('Current variable value :', var)
print ("Good bye!")
▪ Observe the output produced by the following codes.
import random
lcv=1
key=random.randint(1,11)
while lcv<=20:
x=int(input("Enter a value between 1-10: "))
if x==key:
print("you found it.")
break
if x==key-1 or x==key+1:
print("add or subtract 1")
continue
lcv=lcv+1
print("Total trial left ",20-lcv)
print("loop terminates")
▪ Explain the term nested loop
▪ Demonstrate how nested loop works
▪ A loop that executes inside other loop
▪ Construct multiplication table
import time
for i in range (1,3):
print("Multiplication table ",i)
for j in range (1,12):
z=i*j
print("{} x {} = {}".format(i,j,z))
time.sleep(2)
Multiplication table 1
1x1=1
1x2=2
1x3=3
for i in range (1,3): 1x4=4 for j in range (1,12):
1x5=5
i=1 1x6=6 J=1..12
1x7=7 (inner loop)
1x8=8
1x9=9
1 x 10 = 10
1 x 11 = 11
1 x 12 = 12
i=1..2
(outer loop) Multiplication table 2
2x1=2
2x2=4
2x3=6
2x4=8
2 x 5 = 10
i=2 2 x 6 = 12 J=1..12
2 x 7 = 14 (inner loop)
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
2 x 11 = 22
2 x 12 = 24
▪ Complete the following loop so that it can solve the following Sampel Output:
problem: Enter an ID: 1111
Enter sales at Quarter 1: 5000
Read data for 5 salesperson. For each salesperson, request
salesperson ID(4 digits), total sales for 4 quarters in a year. Enter sales at Quarter 2: 1200
Calculate the sum of the total sales for the whole year. Enter sales at Quarter 3:10000
Enter sales at Quarter 4: 20000
for I in range ( ?): Total sales a year for 1111 is 36200
ID=?
sum=0 Enter an ID: 1121
Enter sales at Quarter 1: 500
for j in range(?): Enter sales at Quarter 2: 2000
str1=“Enter sales at Quarter “+str(j) Enter sales at Quarter 3:11000
Enter sales at Quarter 4: 20000
totalsales=int(input(str1)) Total sales a year for 1121 is 33500
sum=?
print(“Total sales a year for {} is {}”.format(ID, sum)) Enter an ID: 1121
:
: