Write a program that takes a value x and then outputs x Fibonnaci numbers. E.g. if x=6 output would be 1 1 2 3 5 8

First define a method that takes a value n and prints the nth number in the Fibonacci sequence. E.g. if n=3....fib(n)=2. ## Here this is done by recursion (a method that calls itself).def fib(n):  if n<=1:    return 1  else:    return fib(n-2) + fib(n-1)## Then define a method to call fib on every number up to the given value x (e.g. fib(0) fib(1) fib(2)) and print the result each timedef fibStarter(x):  for i in range(x):    print(fib(i)) ## Finally call this method to see it working fibStarter(20)

PB
Answered by Peter B. Python tutor

1159 Views

See similar Python Mentoring tutors

Related Python Mentoring answers

All answers ▸

Whats is the difference between a function and a procedure?


What is the difference between a for loop and a while loop?


Write a function that takes an imput (call it usr_in), the user's age (usr_age) and the user's name (usr_nm) and outputs the following string: "Hi, My name is [usr_nm], I am [usr_age] years old, and my favorite number is [usr_in]"


What can I do with Python?


We're here to help

contact us iconContact ustelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2025

Terms & Conditions|Privacy Policy
Cookie Preferences