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 07:30
What are ways to switch windows in excel? check all that apply. on the status bar, click the windows button, and then click the file name. on the task bar, click to display the excel jump list, and then click the file name. on the view tab, in the window group, click switch windows, and then click the file name. on the review tab, in the viewing group, click files, and then click the file name.
Answers: 1
question
Computers and Technology, 23.06.2019 13:10
What is domain name system (dns)? allows dynamic ip address allocation so users do not have to have a preconfigured ip address to use the network converts ip addresses into domains, or identifying labels that use a variety of recognizable naming conventions the efficient coexistence of telephone, video, and data communication within a single network, offering convenience and flexibility not possible with separate infrastructures the integration of communication channels into a single service
Answers: 2
question
Computers and Technology, 24.06.2019 03:00
What is one potential problem associated with an organization purchasing new technology early in its lifecycle
Answers: 1
question
Computers and Technology, 24.06.2019 05:30
Someone plzz me which of these defines a social search? a. asking a search engine a question that is answered by a real person on the other sideb. modifying search results based on popularity of a web pagec.modifying search results based on a ranking of a web page
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
question
Mathematics, 27.01.2021 20:20
question
Mathematics, 27.01.2021 20:20
Questions on the website: 13722367