• Register
57,980 points
11 5 5

Want to solve programming problems and get paid for it? If that sounds interesting to you then contact us.

You have already understood it in part 1 so now let’s modify the code.

Something I didn't want to be doing all the time was modifying the script to designate a maximum limit on the fib (1000) line, this is where it could be said that I start to interact with Python in my own way, which is not exactly the most successful, probably.

I just added a line for the program to ask me the maximum limit of the sequence, that's it:

def fib (n): 
    a, b = 0,1 
    while a <n: 
        print (a, end = '') 
        a, b = b, a + b 
m = int (input ("Enter maximum limit of the sequence: "))      
fib (m)

The int (input ()) line asks me for an input ( input ), see that it is preceded by int that is to say that the value to be entered will be an integer, which is assigned to the variable m —it can be any variable name—.

Finally once again we get to the instruction where the function is called, fib (m) , in this case it contains as an argument m and not 1000, that is, the variable that we assigned previously, and whose value will finally be captured by the first line of code def fib (n) .

Using number of terms

What interested me the most, as I mentioned at the beginning, was to designate the number of terms that I wanted to calculate to obtain the Fibonacci sequence . Now we no longer know what the last value in the sequence will be, for example for 30 terms. But it gives me the control to decide at least how many numbers in the sequence I want to print, as well as being more useful in the case of creating a table.

a, b = 0,1 
for i in range (30): 
    print (a, end = '') 
    a, b = b, a + b

Once again I altered the original code, this time using the for statement instead of while . Both generate a loop, however unlike while , for does not do a Boolean operation to determine if it is true or false , it simply starts a loop executing the lines of code contained within the for loop as many times as specified —iteration—, here I used a range of 30 using the range () function .

for i in range (30):

The result:

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 \ 
6765 10946 17711 28657 46368 75025 121393 196418 317811 514229

Looking for flexibility

Here also I introduced a entry ( input ) not be modifying the code all the time:

def fib (n): 
    a, b = 0,1 
    for i in range (n): 
        print (a, end = '') 
        a, b = b, a + b 
m = int (input ("Enter number of terms for the sequence? ")) 
fib (m)

You could also do it like this:

n = int (input ("Enter number of terms for the sequence?")) 
a, b = 0,1 
for i in range (n): 
    print (a, end = '') 
    a, b = b, a + b

Combine both codes:

sequence = input ("Enter (S) for maximum limit of the sequence \ 
[default number of terms]?") 

if sequence == 'S': 
    def fib (n): 
        a, b = 0,1 
        while a < n: 
            print (a, end = '') 
            a, b = b, a + b 
    m = int (input ("Maximum limit ~>"))    
    fib (m)
 

else: 
n = int (input ("Number of terms ~> ")) 
    a, b = 0,1 
    for i in range (n): 
        print (a, end = '') 
        a, b = b, a + b

In the first line, I am asked what I want to base on for the calculation of the Fibonacci sequence, the data entered is assigned to the variable sequence. In the second line, I add the if statement which makes a comparison and based on it makes a decision.

In this case compares the data stored in the variable succession with S, if the condition is true, is true ( True ), executes the indented code block in if. If it is not fulfilled, it executes the following block of indented code corresponding to else, which refers to the calculation based on the number of terms to obtain the Fibonacci numbers.

Debugging my code

sequence = input ("Enter: (S) maximum limit, (N) number of \ 
terms?") 

if sequence == 'S': 
    def fib (n): 
        a, b = 0,1 
        while a <n: 
            print (a, end = '') 
            a, b = b, a + b 
    m = int (input ("Maximum limit ~>"))    
    fib (m) 

elif sequence == 'N': 
    a, b = 0,1 
    n = int (input ("Number of terms ~>")) 
    for i in range (n): 
        print (i, a) 
        a, b = b, a + b 

else: 
    print ("You must enter Y or N")

Finally, I have minimized the input options to two possibilities "S" or "N" using the elif ( else if ) statement to evaluate a second condition, and as a last resort, if both conditions give false results, continue with the statement else. This means that if no income S or N, any printed character must enter S or N. I also modified print (i, a), which prints the number of terms and the values ​​of the Fibonacci sequence vertically.

57,980 points
11 5 5