-6

Function Name: p​rintFibonacci()​ Your job is to write a function that takes in two parameters and prints out a Fibonacci sequence (In the same line, separated by commas!) using those parameters. A Fibonacci sequence is produced by adding the two preceding numbers together to produce the next integer. The two parameters the user inputs will be the first two numbers you add to start your sequence. The function should stop when the last number printed is more than 300. Remember, the numbers must be printed in the same line with commas separating them. It's okay if the output wraps around to a new line. The key is that you print a single String. You don't need to print the parameters as a part of your output. You can also assume that the user will always input at least 1 non­zero parameter.

It should look like this:

>>> printFibonacci(1,9) 
10,19,29,48,77,125,202,327
>>> printFibonacci(2,3) 
5,8,13,21,34,55,89,144,233,377
python>>> printFibonacci(1,1) 
2,3,5,8,13,21,34,55,89,144,233,377

so far I have this

def printFibonacci(a,b):
    count = 0
    max_count = 10
    while count < max_count:
        count = count + 1
        old_a = a
        old_b = b
        a = old_b
        b = old_a + old_b
        print (old_a)

but it does not print in one line with comas and I don't know how to make it stop at 300.

Ok, so I have worked on it and now I have this which works much better:

def printFibonacci(a,b):
count = 0
maxnumber = 299
while b < 200:
    begin = a+b
    a , b = b , begin
    start = a
    end = b
    print ((start)+ (end),end=",")

I am only having two little problems, one it is printing out a coma at the end of the string as well, how do I get rid of it?? and also the first number it is given me is already the sum of the first two and not the two parameters

Mary
  • 59
  • 6

3 Answers3

2

Do your own homework! (Don't submit this solution; it probably uses concepts you haven't learned yet).

#!/usr/bin/env python2.7

def printFibonacci(a, b):
    while b < 300:
        a, b = b, a+b
        yield b

def main():
    tests = [(1, 9), (2, 3), (1, 1)]
    for test in tests:
        print ', '.join(str(n) for n in printFibonacci(*test))

if __name__ == '__main__':
    main()

Output:

10, 19, 29, 48, 77, 125, 202, 327

5, 8, 13, 21, 34, 55, 89, 144, 233, 377

2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377

Community
  • 1
  • 1
pzp
  • 6,031
  • 1
  • 26
  • 38
  • I have changed things on my code, can you help me please? – Mary Jun 03 '15 at 02:46
  • Wow! See what you accomplished just by trying! Now for the final tweaking... I deleted ``count``, ``maxnumber``, ``begin``, ``start``, and ``end`` because you weren't using those variables. Next I corrected a typo in your while loop condition (you wrote 200, but I'm almost positive you meant to write 300). And finally, instead of printing ``a + b`` I made it print just ``b``. The rest of the code is all yours! Congrats! Here's a [pastebin](http://pastebin.com/fKGRAc36) with my corrections. – pzp Jun 03 '15 at 03:08
0

I'm also learning Python. I'm not in a class or anything, but feel free to review the code and give me feedback.

#!/usr/bin/python
#fib.py

#array to hold series of fib numbers
fibSequence = []

#expects two integers, the lesser number first
def printFibonacci(lesserNumber, greaterNumber):
    #access the array declared outside the function
    global fibSequence

    #check if the list is empty; if it is then start it
    if not fibSequence:
        fibSequence=[lesserNumber, greaterNumber]

    #the next number in the sequence will be the sum of the two parameters
    sumNumber = lesserNumber + greaterNumber

    if greaterNumber> 300:
        print ",".join(str(i) for i in fibSequence)
    else:
        #add the summed number to the list that will eventually be our answer
        fibSequence.append(sumNumber)
        #call the function again, this time with the formerly greatest number as the smaller number
        printFibonacci(greaterNumber, sumNumber)

#call the function for the first time
printFibonacci(2,3)
user1717828
  • 6,812
  • 6
  • 31
  • 52
-1

Usually fibonacci type problems are most easily solved with recursion:

def printFibonacci(x,y):

    if y > 300:
        return
    z = x + y
    print z,
    printFibonacci(y,z)
    return
Bhargav Rao
  • 45,811
  • 27
  • 120
  • 136
anon
  • 1