Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help!
#1
Please, can someone help me with this problem:

The Fibonacci sequence is a series of related numbers. Usually, the series begins with the numbers 0 and 1, and every successive number is calculated as the sum of the previous two numbers. So the first 11 elements of the series are:
0 1 1 2 3 5 8 13 21 34 55
You must write a program that contains a function called fibonacci_index This function should receive as parameter the index (or position) that you want to know within the Fibonacci series. The function must return the Fibonacci number in that index. For example, if index 6 is requested, the result must be 5, and if index 11 is requested, the result must be 55.
Reply
#2
So, what have you tried so far?
Reply
#3
This:
n = int(input())
def fib(n):
a=0
b=1
while a < n:
print(a)
a, b = b, a+b
print()
fib(n)

it does list the sequence 'till "n". But I only need the last element and I don't know how to get it.

n = int(input())
def fib(n):
    a=0
    b=1
    while a < n:
        print(a)
        a, b = b, a+b
    print()
fib(n)
just learned I could do this, sorry
Reply
#4
Please, can someone help me with this problem:

The Fibonacci sequence is a series of related numbers. Usually, the series begins with the numbers 0 and 1, and every successive number is calculated as the sum of the previous two numbers. So the first 11 elements of the series are:
0 1 1 2 3 5 8 13 21 34 55
You must write a program that contains a function called fibonacci_index This function should receive as parameter the index (or position) that you want to know within the Fibonacci series. The function must return the Fibonacci number in that index. For example, if index 6 is requested, the result must be 5, and if index 11 is requested, the result must be 55.

SOS!! The deadline is in 40 min!!!

20 min...

I swear I'm also trying, but I'm stuck! that's why I'm asking for help
Reply
#5
I never took a programming class, but it seems strange to be given an assignment with functions and loops, but not having covered lists and .append().

How many days did you have to do this?

google "python index" and click the first link.
Reply
#6
like 3, but I also had like 5 other homeworks
Reply
#7
Well, hand it in late.

But append the result of each loop to a list, then feed the index you want to the function. The function can take that and plug it into
print(your_fib_list[index])
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020