Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fibonnaci
#11
You changed your code, the problem is now in line 7. The while loop should continue as long as the fib number is less than the_number_has_to_be_bigger_than, but it is using position_in_fib_sequence instead. So, the fib sequence is 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... Say I want the second fib number greater than 8. Your code will give me the second fib number greater than 2, which is 8.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#12
Ooh I understand!!

It is working for a part. If I want the 2nd fibonacci number bigger than 4 it is giving 8. But if I give the 2nd bigger than 5 it gives also 8.

position_in_fib_sequence = int(input("Which fibonaccinumber do you want to know?"))
the_number_has_to_be_bigger_than = int(input("Bigger than?"))
  
G1 = 0
G2 = 1
 
while G2 < the_number_has_to_be_bigger_than:
    G1, G2 = G2, G1 + G2
  
for i in range(position_in_fib_sequence-1):
    G1, G2 = G2, G1 + G2
	
print("The", str(position_in_fib_sequence) + "th", "fibonaccinumber bigger than", the_number_has_to_be_bigger_than, "is", G2)
Reply
#13
That's because 5 is a fibonacci number. You want the < on line 7 to be a <=, so that coming out of that loop the current fib number is guaranteed to be larger than the number provided.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#14
(Feb-28-2019, 07:32 PM)ichabod801 Wrote: That's because 5 is a fibonacci number. You want the < on line 7 to be a <=, so that coming out of that loop the current fib number is guaranteed to be larger than the number provided.

Thank you for your help and let me understand! I just began programming so it's still difficult for me.
Reply


Forum Jump:

User Panel Messages

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