Norman Bird - 6 months ago 11

Python Question

Please see image below for my references.

The first column just gives us the row numbers

2nd column is the Fibonacci sequence, just adding the previous 2 to get the new row

3rd column should be the quotient of last row b divided by previous a value

It works up until the 4th iteration where the fraction should be:

1.6, but it is 1.0000000 from then on

3rd col should be

`0`

1

2

1.66666666667

1.6

1.625 etc etc.

But it is doing

`0`

1

2

1.0000

1.0000

1.0000

1.0000 etc.

What is wrong with my code? It should work but isn't. Here is python 3 code:

`a, b = 0, 1`

num = 1

i = 1

f= 0

#loop

for i in range(1, 101):

#use if to avoid divide by zero initially because a is 0

if a >= 1:

f = b / a

#print 3 column of 3 different numbers

print ('{0:4d} {1:20d} {2:1.14f}'.format(i, b, f) )

i = i + 1

a,b = b, a+b

Answer

If you're using Python 2, change the line:

```
f = b / a
```

into

```
f = float(b) / float(a)
```

Are you sure you're using Python 3? What does `python -V`

report?

Source (Stackoverflow)

Comments