FibonacciΒΆ
# set first value
a0 = 1
print(a0)
# set second value
a1 = 1
print(a1)
# calculate third value
a2 = a1+a0
print(a2)
while a2 <= 100:
a0 = a1 # set a0 to the previous value of a1
a1 = a2 # set a1 to the previous value of a2
a2 = a1+a0 # re-calculate a2
print(a2)
The loop is executed for the last time when
a2==89
, so the last value given to theprint
function isa2=55+89
to correct this try the moving the print to before the iteration:
a0 = 1
print(a0)
a1 = 1
print(a1)
a2 = a1+a0
#print(a2)
while a2 <= 100:
print(a2)
a0 = a1 # set a0 to the previous value of a1
a1 = a2 # set a1 to the previous value of a2
a2 = a1+a0 # re-calculate a2
# the value that's over 100 never gets printed here (even though it is calculated)