Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fibonnaci
#7
You've got an indentation problem in the code shown. The three lines at the end need to be unindented one level.

Once that is fixed, that loop should be over range(n2 - 1). A for loop over range(x) will repeat x times. If you want the n2th Fibonacci number after n1, the while loop gives you the first, so that is why you subtract one.

The for loop should do exactly what the while loop is doing. It should keep generating Fibonacci numbers that same way.

Note the way you are calculating the next Fibonacci number on lines 10-12. There's nothing wrong with it. But that changing one number and then swapping is such a common thing to do that Python has a shortcut for it called tuple assignment. That is, if you have a tuple (comma separated values) on both sides of an assignment (=), the items will be assigned in order. a, b = 1, 2 assigns 1 to a and 2 to b. So you can do those three lines in one line:

G1, G2 = G2, G1 + G2
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
Fibonnaci - by PythonNoob99 - Feb-21-2019, 09:07 PM
RE: Fibonnaci - by nilamo - Feb-21-2019, 10:17 PM
RE: Fibonnaci - by PythonNoob99 - Feb-22-2019, 03:19 PM
RE: Fibonnaci - by ichabod801 - Feb-22-2019, 03:28 PM
RE: Fibonnaci - by PythonNoob99 - Feb-22-2019, 03:32 PM
RE: Fibonnaci - by PythonNoob99 - Feb-26-2019, 11:43 AM
RE: Fibonnaci - by ichabod801 - Feb-26-2019, 04:39 PM
RE: Fibonnaci - by PythonNoob99 - Feb-26-2019, 07:39 PM
RE: Fibonnaci - by ichabod801 - Feb-27-2019, 04:32 AM
RE: Fibonnaci - by PythonNoob99 - Feb-27-2019, 05:28 PM
RE: Fibonnaci - by ichabod801 - Feb-27-2019, 06:15 PM
RE: Fibonnaci - by PythonNoob99 - Feb-28-2019, 08:35 AM
RE: Fibonnaci - by ichabod801 - Feb-28-2019, 07:32 PM
RE: Fibonnaci - by PythonNoob99 - Feb-28-2019, 07:46 PM

Forum Jump:

User Panel Messages

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