-5

I'm struggling to work out what this function in python actually does?

def mystery(n):
    a, b = 0, 1
    while a < n:
        print(a)
        a, b = b, a + b

Although basic I don't fully understand what it is achieving? When adding a basic command to run it with the letter N such as-

def mystery(n):
    a, b = 0, 1
    while a < n:
        print(a)
        a, b = b, a + b

n = int(input("Input the letter N"))
mystery(n)

It comes up with the strangest outputs, such as-

Input the letter N 20
0
1
1
2
3
5 
8
13

Please help me understand this code,

Thanks, Isaac.

Arc676
  • 4,445
  • 3
  • 28
  • 44
user205757
  • 1
  • 1
  • 3
  • You should include sample input and output in your post. You can [edit] and include this information. – Arc676 Jan 25 '16 at 09:35
  • 1
    It's `fibonacci` numbers till reaches `n` – ᴀʀᴍᴀɴ Jan 25 '16 at 09:37
  • I might be expecting too much as I don't know anything about your coding or math abilities, but if you simply write out a table with `a` and `b` then run the code by hand for any value `n`, you will quickly see that this is the Fibonacci sequence. – Arc676 Jan 25 '16 at 09:37
  • Cheers! @Arman Looked it up and it works out thanks. – user205757 Jan 25 '16 at 09:41
  • For the record, if you ever search for anything else regarding this, it's called a "dry run". It's where you run through the code (not necessarily by hand) and keep track of all the variables and output. – Arc676 Jan 25 '16 at 09:43
  • @Arc676 It does mention doing a "Dry Run" on a little man computer programme later on in this paper I'm doing, thanks for all the help! – user205757 Jan 25 '16 at 09:45
  • When confronted to a sequence of numbers, you might want to check if it's a known mathematical function or object with the series encyclopedia : https://oeis.org/ – DainDwarf Jan 25 '16 at 10:18

1 Answers1

1

To summarize:

The function prints out the first N numbers in the Fibonacci sequence.

This is a sequence starting with 1, 1 and each following term is the sum of the two previous terms.

a, b = b, a + b

Here, you have 2 variables. a is always the current term and b is the next term. Each iteration, after printing the current term, you assign the next term to a and calculate the term after that.

a: current term
b: the next term
a+b: the term after that

You can read more about swapping variables using this method on this SO post.

From the accepted answer by @eyquem:

Python evaluates expressions from left to right. Notice that while evaluating an assignment, the right-hand side is evaluated before the left-hand side.

Documentation: Expressions § Evaluation Order

Community
  • 1
  • 1
Arc676
  • 4,445
  • 3
  • 28
  • 44
  • You've just done someone's homework ;) – geotheory Jan 25 '16 at 09:52
  • @geotheory True, but a) it's good practice for me to explain things to make sure I understand them and b) it seems the question was resolved in the comments anyway, which is discouraged by the tooltip text of the "add comment" button. Answering seems to be the ending step in the cycle that is Q&A, so since a question was asked an answer will eventually be given :) – Arc676 Jan 25 '16 at 09:55
  • 1
    I accept some of the logic, except that we now have `Python “Mystery” Code` clogging up the SO namespace for google search.. – geotheory Jan 25 '16 at 10:14
  • Thank you very very much! This has been a great help! – user205757 Jan 28 '16 at 09:30