subject

#The Fibonacci sequence is a number sequence where each #number is the sum of the previous two numbers. The first #two numbers are defined as 0 and 1, so the third number is #1 (0 + 1 = 1), the fourth number is 2 (1 + 1 = 2), the #fifth number is 3 (1 + 2 = 3), the sixth number is 5 #(2 + 3 = 5), and so on. #
#Below we've started a class called FibSeq. At any time, #FibSeq holds two values from the Fibonacci sequence: #back1 and back2.
#
#Create a new method inside FibSeq called next_number. The #next_number method should:
#
# - Calculate and return the next number in the sequence, # based on the previous 2. # - Update back2 with the former value of back1, and update # back1 with the new next item in the sequence.
#
#This means that consecutive calls to next_number should #yield each consecutive number from the Fibonacci sequence. #Calling next_number 5 times would print 1, 2, 3, 5, and 8.
class FibSeq:
def __init__(self):
self. back1 = 1
self. back2 = 0
def next_number(self):
self. back1=self. back1+self. back2 # updated the back1 value to the next number in the series first
self. back2=self. back1-self. back2 #updated the back2 value with previous back1 value
yield (self. back1) # yielded the next number in the series since it is updated as back1 so yielded back1
f = FibSeq()
for i in range(5): # here i have iterated the series only 5 times u can change it as you like
s=f. next_number()
print(next(s))# here next is an iterator function for the yield generator.
#The code below will test your method. It's not used for
#grading, so feel free to change it. As written, it should
#print 1, 2, 3, 5, and 8.
newFib = FibSeq()
print(newFib. next_number())
print(newFib. next_number())
print(newFib. next_number())
print(newFib. next_number())
print(newFib. next_number())

ansver
Answers: 1

Another question on Computers and Technology

question
Computers and Technology, 23.06.2019 12:00
From excel to powerpoint, you can copy and paste a. cell ranges and charts, one at a time. b. cell ranges and charts, simultaneously. c. charts only. d. cell ranges only.
Answers: 3
question
Computers and Technology, 23.06.2019 22:20
What is a programming method that provides for interactive modules to a website?
Answers: 1
question
Computers and Technology, 25.06.2019 02:00
What type of multimedia is a game cd? game cds are examples of multimedia
Answers: 1
question
Computers and Technology, 25.06.2019 07:00
Amisfire code is a type diagnostic trouble code (dtc).
Answers: 1
You know the right answer?
#The Fibonacci sequence is a number sequence where each #number is the sum of the previous two numbe...
Questions
question
Mathematics, 25.06.2019 21:10
question
Mathematics, 25.06.2019 21:10