I'm trying to use this code for a fibonocci sequence where I'm setting the starting numbers
as b= and c=. What I'd like to do though is have it stop after so many numbers are added together, in this case it would be 53rd (a=53). I've tried a 'for i in range(53):'loop but
that doesn't work for this. The a,b,c numbers can be randomly choosen, which I
have working, but for this snippet I'm setting them as static. Suggestions?

a =  53
b = 7
c = 18


k, l = b, c
while l < 9999999999999999:
    xa = l
    k,l = l, k+l
    print xa,

   #for i in range(a):
         #print i

    #for x in xa: ??

I've tried a 'for i in range(53):'loop but
that doesn't work for this

Why not? It seems like the proper way to do this. Note that you salt the variables with the first 2 numbers, so the loop would run 2 less than the number of the total sequence.

Also, do not use i, O, l, etc. as variable names as they look like numbers, i.e. k+l, is that k+el or k+one?

a =  23
b = 7
c = 18

print 1, b
print 2, c
for ctr in range(a-2):
    b, c = c, b+c
    print ctr+3, c

I see...I was apparently doing something wrong because my 'for' loop kept throwing an 'index out of range' error for me. Thanks, that works the way I need it to and point taken on the variables! :)

Why not simply use itertools.islice and a fibonacci generator?

wooee show that range() work fine,another good and pythonic way is to use a generator.
If also use itertools.islice() can make it even more versatile.

from itertools import islice

def fib(a=7, b=18):
    yield a
    while True:
        yield b
        a, b = b, a + b

fib_numb_1 = list(islice(fib(),15))
# Slice out last 3 number
fib_numb_2 = list(islice(fib(),12,15))
# Slice out number with a step of 2
fib_numb_3 = list(islice(fib(),0,23,2))
#---
print fib_numb_1
print fib_numb_2
print fib_numb_3

'''Output-->
[7, 18, 25, 43, 68, 111, 179, 290, 469, 759, 1228, 1987, 3215, 5202, 8417]
[3215, 5202, 8417]
[7, 25, 68, 179, 469, 1228, 3215, 8417, 22036, 57691, 151037, 395420]
'''

No list output,is of course just to loop over generator object.

for i in islice(fib(),15):
    print i

7
18
25
43
68
111
179
290
469
759
1228
1987
3215
5202
8417

With count trow in enumerate().

for index, item in enumerate(islice(fib(),15), 1):
    print '{} -> {}'.format(index, item)

1 -> 7
2 -> 18
3 -> 25
4 -> 43
5 -> 68
6 -> 111
7 -> 179
8 -> 290
9 -> 469
10 -> 759
11 -> 1228
12 -> 1987
13 -> 3215
14 -> 5202
15 -> 8417

The xfibo() function is fast, so you might as well use:

import itertools

def xfibo():
    """
    a generator for Fibonacci numbers, goes
    to next number in series on each call
    """
    current, previous = 0, 1
    while True:
        yield current
        # use a tuple swap
        current, previous = previous, current + previous

# to get selected results of a generator function use
print( "show Fibonacci series 77 through 80:" )
for k in itertools.islice(xfibo(), 77, 81):
    print( k )

"""my result -->
    show Fibonacci series 77 through 80:
5527939700884757
8944394323791464
14472334024676221
23416728348467685
"""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.