Answer to Question #343959 in Python for Dhea M. Maddela

Question #343959

Create a Python script that will display the next 5 numbers in the sequence where each number is the sum of the previous two.


This is my text file:

8 3

4 5

6 2


The sample output should be like this when I run the code:

8 3 11 14 25 39 64

4 5 9 14 23 37 50

6 2 8 10 18 28 46


I need the code to have the python script that will display the next 5 numbers from my text file stated above.


1
Expert's answer
2022-05-23T10:19:34-0400
def print_5_nums (n1, n2):
    print(n1, end=' ')
    for i in range(5):
        n1, n2 = n2, n1 + n2
        print(n1, end=' ')
    print(n2)


def read_and_print(filename):
    with open(filename) as txt:
        for line in txt:
            n1, n2 = line.split()
            print_5_nums (int(n1), int(n2))


read_and_print('my.txt')

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS