Python Loop
-
How to write a script that uses a control statement (such as FOR loops, WHILE loops, etc...) to print out 50 lines using variables as shown in Figure 1. Note that Line 3 is derived by adding Line 1 & 2. Line 4 is derived by adding Line 2 & 3 and so on… Line 1 : 1 Line 2 : 1 Line 3 : 2 Line 4 : 3 Line 5 : 5 Line 6 : 8 Line 7 : 13 Line 8 : 21 … … Figure1. Output from executed script :confused: Smaini
-
How to write a script that uses a control statement (such as FOR loops, WHILE loops, etc...) to print out 50 lines using variables as shown in Figure 1. Note that Line 3 is derived by adding Line 1 & 2. Line 4 is derived by adding Line 2 & 3 and so on… Line 1 : 1 Line 2 : 1 Line 3 : 2 Line 4 : 3 Line 5 : 5 Line 6 : 8 Line 7 : 13 Line 8 : 21 … … Figure1. Output from executed script :confused: Smaini
q = [0,1]
for i in range(1,50):
sum = q[0] + q[1]
print 'Line %d : %d' % (i,sum)
if i > 1:
q = [ q[1], sum ]MBH
-
q = [0,1]
for i in range(1,50):
sum = q[0] + q[1]
print 'Line %d : %d' % (i,sum)
if i > 1:
q = [ q[1], sum ]MBH
Ok thanks for your suggestion!! :-D