python - how to print out fibonacci in this format? -
function name: printfibonacci() job write function takes in 2 parameters , prints out fibonacci sequence (in same line, separated commas!) using parameters. fibonacci sequence produced adding 2 preceding numbers produce next integer. 2 parameters user inputs first 2 numbers add start sequence. function should stop when last number printed more 300. remember, numbers must printed in same line commas separating them. it's okay if output wraps around new line. key print single string. don't need print parameters part of output. can assume user input @ least 1 nonzero parameter.
it should 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 have this
def printfibonacci(a,b): count = 0 max_count = 10 while count < max_count: count = count + 1 old_a = old_b = b = old_b b = old_a + old_b print (old_a)
but not print in 1 line comas , don't know how make stop @ 300.
ok, have worked on , have works better:
def printfibonacci(a,b): count = 0 maxnumber = 299 while b < 200: begin = a+b , b = b , begin start = end = b print ((start)+ (end),end=",")
i having 2 little problems, 1 printing out coma @ end of string well, how rid of it?? , first number given me sum of first 2 , not 2 parameters
do own homework! (don't submit solution; uses concepts 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)] test in tests: print ', '.join(str(n) 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
Comments
Post a Comment