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, 22.06.2019 11:50
You have written, as part of a school assignment, a research paper on the solar system. you want to share this paper on your school website. on which type of server will you upload it?
Answers: 1
question
Computers and Technology, 22.06.2019 22:30
The qwerty keyboard is the most common layout of keys on a keyboard
Answers: 3
question
Computers and Technology, 23.06.2019 18:30
Write a program that prints the day number of the year, given the date in the form month-day-year. for example, if the input is 1-1-2006, the day number is 1; if the input is 12-25-2006, the day number is 359. the program should check for a leap year. a year is a leap year if it is divisible by 4, but not divisible by 100. for example, 1992 and 2008 are divisible by 4, but not by 100. a year that is divisible by 100 is a leap year if it is also divisible by 400. for example, 1600 and 2000 are divisible by 400. however, 1800 is not a leap year because 1800 is not divisible by 400.
Answers: 3
question
Computers and Technology, 23.06.2019 21:30
Examine the list below. which factors positively affect lifetime income? check all that apply.
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, 27.01.2021 03:30
Questions on the website: 13722359