Topic: writing functions for numbert patterns

Hi, how would you write a function that would allow you to have a pattern print infinitely or stop at a specific number. I'm writing  code for the fibonacci sequence and I'm having trouble figuring it out.

Thumbs up

Re: writing functions for numbert patterns

If you're asking for code snippets then I've made a few.
This will return the nth Fibonacci number.

def fib(n):
    a,b,d=0,1,0
    if n == 0:
    return 1
    while d<n:
        c=a+b
        a=b
        b=c
        d+=1
    return c
fib(input('nth Fibonacci number: '))

For the series in a list up to the nth Fibonacci number then

def fib0(n):
    a,b,d,l=0,1,0,[]
    if n == 0:
        l=[1]       
        return l
    while d<n:
        c=a+b
        a=b
        b=c
        l.append(c)
        d+=1
    return l
fib(input('series up to the nth Fibonacci number (input n): '))

for the series to repeat infinity

def fib1():
    a,b=0,1
    while 1:
        c=a+b
        a=b
        b=c
        print(c)
        raw_input()
print('if you use ctrl+c the last value was assigned to variable c.')
fib1()

just copy and paste this into idle or your python shell (if you paste it into your python shell be sure to remove the parts other than that function)

Thumbs up