CH 6 FLOW OF CONTROL TB Exercise & Extra Questions
CH 6 FLOW OF CONTROL TB Exercise & Extra Questions
(vi) var = 7
while var > 0:
print ('Current variable value: ', var)
var = var -1
if var == 3:
break
else:
if var == 6:
var = var -1
continue
print ("Good bye!")
Ans: Current variable value:7
Current variable value:5
Good bye!
Current variable value:4
IMPORTANT QUESTIONS
How many times is the following loop executed?
1. i=100
while i<=200:
print(i)
i+=20
Ans : 6 times
2. for a in range(100,10,-10)
print(a)
Ans: 9 times
3. i=0
while i<0 and i>2:
print(“Hello..”)
i=i+1
Ans: 0 times
4. x=45
while x<50:
print(x)
Ans: Infinite times
5. n=25
while n>10:
print(n*2)
n-=3
Ans: 5 times
a=25
for a in range (25,500,25): while a<500:
2 print(a) print(a)
a=a+25
a=90
for a in range(90,9,-9):
while a>9:
3 print(a)
print(a)
a=a-9
c=0
c=0 d=2
for d in range(2,10,2): while d<10:
print(c*d) print(c*d)
4
c+=1 c+=1
print(“Series Over”) d+=2
print(“Series over”)
ERROR CORRECTION
Rewrite the following code in Python after removing all syntax
error(s).Underline each correction done in the code.
countdown=10 countdown=10
while countdown>0 while countdown>0:
print countdown print (countdown)
1
countdown-1 countdown=countdown-1
print(“Finally”) print(“Finally”)
Limit=40 Limit=40
for M in range[0,Limit]: for M in range(0,Limit):
if M%5=0: if M%5==0:
2 print(M*2) print(M*2)
else else:
print(m+2) print(M+2)
250=Number Number=250
WHILE Number<=1000: while Number<=1000:
if Number=>750: if Number>=750:
print Number print (Number)
3 Number=Number+100 Number=Number+100
else else:
print Number*2 print (Number*2)
Number=Number+50 Number=Number+50
30=To To=30
for k in range(0,To) for k in range(0,To):
IF k%4==0: if k%4==0:
print(k*4) print(k*4)
4
Else: else:
print(k+3) print(k+3)
1 x=10 10 0
y=0 91
while x>y: 82
print(x,y) 73
x=x-1 64
y=y+1
2 if the input is 6: 1
n=int(input(“Enter an integer:”)) 4
if n<1: 9
print(“Invalid value”) 16
else: 25
for i in range(1,n+1): 36
print(i*i)
3 x,y=5,20 5 20 4
while x<y: 8 17 9
if y%x==0: 11 14 3
z=y//x
else:
z=y-x
print(x,y,z)
x+=3
y-=3
4 sum,step=0,5 45 @ 20
for i in range(0,6,2):
step+=5
sum+=step
print(sum,”@”,step)
5 for a in “abcde”: a+b+c+d+e
print(a,’+’,end=’ ‘)
6 for i in range(0,10): 9
pass
print(i)
11 N=int(input(“Enter N:”)) a. 6
i=1 b. 0
sum=0
while i<N:
if i%2==0:
sum=sum+i
i=i+1
print(sum)
**********************