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

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 10:40, almaga1979orfvwo
5. illustrate how fine-line inventory classification can be used with product and market segments. what are the benefits and considerations when classifying inventory by product, market, and product/market?
Answers: 2
image
Computers and Technology, 22.06.2019 19:20, mahaleyrenee1195
How might the success of your campaign be affected if you haven’t carefully completed all field data or if you accidentally insert the wrong merge field in the document?
Answers: 2
image
Computers and Technology, 22.06.2019 22:00, bbysl15
What is the name of the option in most presentation applications with which you can modify slide elements? 1. the option enables you to modify a slide element in most presentation applications.
Answers: 3
image
Computers and Technology, 23.06.2019 14:00, LuvIsRage2
Technician a says that with self-adjusting clutch systems, the release bearing constantly rotates. technician b says that the ball bearing portion of the release bearing should be lubricated with high-temperature grease during routine maintenance. which technician is correct?
Answers: 2
You know the right answer?
#The Fibonacci sequence is a number sequence where each #number is the sum of the previous two numbe...

Questions in other subjects:

Konu
Mathematics, 12.12.2020 17:50
Konu
Medicine, 12.12.2020 17:50