I am having diffculty to understand this program
<p>i just came across this program and it seems to me very difficult to understand.
which is calculating the Fibonacci boundary for a positive number, returns a 2-tuple. The first element is the Largest Fibonacci Number smaller than x and the second component is the Smallest Fibonacci Number larger than x. The return value is immediately stored via unpacking into the variables lub and sup:</p>

<pre><code>def fib_intervall(x):
    """ returns the largest fibonacci
    number smaller than x and the lowest
    fibonacci number higher than x"""
    if x &lt; 0:
        return -1
    (old,new, lub) = (0,1,0)
    while True:
        if new &lt; x:
            lub = new 
            (old,new) = (new,old+new)
        else:
            return (lub, new)

while True:
    x = int(input("Your number: "))
    if x &lt;= 0:
        break
    (lub, sup) = fib_intervall(x)
    print("Largest Fibonacci Number smaller than x: " + str(lub))
    print("Smallest Fibonacci Number larger than x: " + str(sup))
</code></pre>

<p>I am trying to understand but i am lacking somewhere to understand it properly some big doubts:</p>

<p>why its returning -1 ?
what  this line  <code>(old,new, lub) = (0,1,0)</code> doing ?? 
what this piece of code intend to do ? </p>

<pre><code> if new &lt; x:
            lub = new 
            (old,new) = (new,old+new)
        else:
            return (lub, new)
</code></pre>

<p>How this assignment works in python ??   <code>(lub, sup) = fib_intervall(x)</code></p>

<p>overall if you explain whole program with deep explanation that would be so helpful.</p>

<p>Thanks in advance.</p>

<pre><code>if x &lt; 0:
    return -1
</code></pre>

<p>This is checking if the number is a positive integer bigger than one. Since Fib needs to be a positive integer. </p>

<pre><code> if x &lt;= 0:
    break
</code></pre>

<p>Uses the above to check if it should print anything out (if there is a valid output to print out) If the input is not valid, it won't print anything out. </p>

<pre><code> (old,new, lub) = (0,1,0) 
</code></pre>

<p>Is a silly way to unpack the tuple <code>(0,1,0)</code> to the variables <code>old,new,lub</code>. The <code>(old,new,lub)</code> is a tuple of variables but we aren't interested in that. Since this code still work with just rewriting it to be:</p>

<pre><code> old,new,lub = 0,1,0
</code></pre>

<p>You can think of it as doing:</p>

<pre><code> old = 0
 new = 1
 lub = 0
</code></pre>

<p>Same thing in this case. </p>

<p>The overall program just checks if there's a valid input and outputs the correct values according to what the strings says. </p>

<pre><code>while True:
    if new &lt; x:
        lub = new 
        (old,new) = (new,old+new)
    else:
        return (lub, new)
</code></pre>

<p>This is the heart of the program, it loops until <code>new</code> is smaller than <code>x</code>. If it's not it makes <code>lub</code> to the current <code>new</code> and just increment <code>old</code> and <code>new</code> to be the new values of <code>new</code> and <code>old+new</code>. In basic terms, it's doing calculation on what the biggest value and smallest number. Just read the code, it seems very self explaining after the first two questions </p>

<pre><code>(old,new) = (new,old+new)
</code></pre>

<p>Is like above, where the <code>()</code> are useless here since it's not doing anything. The above can be rewritten or thought as </p>

<pre><code>old = new
new = old+new
</code></pre>

<p>Edited for more questions in Comments: </p>

<pre><code>(lub, sup) = fib_intervall(x)
</code></pre>

<p>This is calling the function fib_intervalls, and unpacking the return values of what it returns <code>return (lub, new)</code>. Notice the function returns 2 variables in a tuple: <code>lub, new</code>. So when you call the function <code>fib_intervall(x)</code> it returns <code>(lub,new)</code> which is now unpacked and assign to <code>lub,sup</code>. Basically you can think of it like doing:</p>

<pre><code>returned_values = fib_intervall(x)
lub = returned_values[0]
sup = returned_values[1]
</code></pre>

<p>This code is calling the function under the while condition because it wants the user to be able to loop and asking multiple inputs until an input is invalid. That's the point of functions, reuse able code. </p>

<p>Also you only <code>def function():</code> when you are making a new function. Look at the first line of code:</p>

<pre><code> def fib_intervall(x):
</code></pre>

<p>This is how you define a function, and to call it you simply use <code>fib_intervall(input)</code>. Make sure you get the return values like the script is or running the function here will be pointless. </p>

python python-2.7 python-3.x