Answer:
Here is the next_number method:
def next_number(self): #method to return next number in the sequence
temporary = self.back1 + self.back2 # adds previous number to next number and stores the result in a temporary variable
self.back2 = self.back1 #Updates back2 with the former value of back1,
self.back1 = temporary #update back1 with the new next item in the sequence.
return temporary #
Step-by-step explanation:
I will explain the working of the above method.
back1 = 1
back2 = 0
At first call to next_number() method:
temporary = back1 + back2
= 1 + 0
temporary = 1
self.back2 = self.back1
self.back2 = 1
self.back1 = temporary
self.back1 = 1
return temporary
This return statement returns the value stored in temporary variable i.e. 1
Output: 1
back1 = 1
back2 = 1
At second call to next_number() method:
temporary = back1 + back2
= 1 + 1
temporary = 2
self.back2 = self.back1
self.back2 = 1
self.back1 = temporary
self.back1 = 2
return temporary
This return statement returns the value stored in temporary variable i.e. 2
output: 2
back1 = 2
back2 = 1
At second call to next_number() method:
temporary = back1 + back2
= 2 + 1
temporary = 3
self.back2 = self.back1
self.back2 = 2
self.back1 = temporary
self.back1 = 3
return temporary
This return statement returns the value stored in temporary variable i.e. 3
Output: 3
back1 = 3
back2 = 2
At second call to next_number() method:
temporary = back1 + back2
= 3 + 2
temporary = 5
self.back2 = self.back1
self.back2 = 3
self.back1 = temporary
self.back1 = 5
return temporary
This return statement returns the value stored in temporary variable i.e. 5
Output: 5
back1 = 5
back2 = 3
At second call to next_number() method:
temporary = back1 + back2
= 5 + 3
temporary = 8
self.back2 = self.back1
self.back2 = 5
self.back1 = temporary
self.back1 = 8
return temporary
This return statement returns the value stored in temporary variable i.e. 8
Output: 8
Calling next_number 5 times would print 1, 2, 3, 5, and 8.
The complete program along with its output is attached.