Python中的While循环和Fibonacci(不导入或使用“fib”)

Python中的While循环和Fibonacci(不导入或使用“fib”),python,while-loop,fibonacci,Python,While Loop,Fibonacci,我试着用变量来寻找所有斐波那契函数,如果可以被一个选择的变量除掉的话,我可以把它们加起来。我还想尝试在不使用任何fib()或备忘录的情况下编写它 这是我的密码: endNum = int(raw_input("Enter the end number here ")) divisable_by = int(raw_input("Sum all numbers in the sequence that divide by: ")) # below is the same as "a, b

我试着用变量来寻找所有斐波那契函数,如果可以被一个选择的变量除掉的话,我可以把它们加起来。我还想尝试在不使用任何fib()或备忘录的情况下编写它

这是我的密码:

endNum = int(raw_input("Enter the end number here "))
divisable_by = int(raw_input("Sum all numbers in the sequence that divide by: "))   

# below is the same as "a, b = 0, 1"
a = 0
b = 1
""" gave single letter variables to those above to use in formula below
(longer strings do not seem to work)"""
c = endNum
d = divisable_by

# while b is less than or equal to the ending number, it will loop.
while b <= c:
    print b
# below is the same as "a, b = b, a+b"
    a_old = a
    a = b
    b = a_old + b
# the below helps with summing the numbers that are divisable by number chosen.
    total = 0
    for i in range(a, c):
        if i%d == 0:
            total += i
#prints text and number calculated above.
print "Sum of all natural numbers that divide by the number you chose" 
print "and are less than the other number you chose is: ", total
endNum=int(原始输入(“在此处输入结束编号”))
可除的除以=int(原始输入(“序列中除:”)的所有数字之和)
#以下内容与“a,b=0,1”相同
a=0
b=1
“”“为上面的变量提供了单字母变量,以便在下面的公式中使用
(较长的字符串似乎不起作用)”
c=结束数
d=可除以
#当b小于或等于结束数时,它将循环。

而b在每个循环中,您都将total重置为0。把它移出循环。也不要在每个循环中测试范围。这也会给你错误的结果。只测试找到的新数字。

您需要先将斐波那契数字添加到列表中,以便以后在进行除法检查时使用。第二,你需要把总数从循环中去掉,因为正如kgiannakakis所说,它总是被0代替。所以它应该是这样的:

endNum = int(raw_input("Enter the end number here "))
divisable_by = int(raw_input("Sum all numbers in the sequence that divide by: "))   

# below is the same as "a, b = 0, 1"
a = 0
b = 1
""" gave single letter variables to those above to use in formula below
(longer strings do not seem to work)"""
c = endNum
d = divisable_by

fib_list = []
# while b is less than or equal to the ending number, it will loop.
while b <= c:
    print b
    fib_list.append(b)
# below is the same as "a, b = b, a+b"
    a_old = a
    a = b
    b = a_old + b
# the below helps with summing the numbers that are divisable by number chosen.
total = 0
for i in fib_list:
  if i%d == 0:
    total += i
#prints text and number calculated above.
print "Sum of all natural numbers that divide by the number you chose" 
print "and are less than the other number you chose is: ", total
endNum=int(原始输入(“在此处输入结束编号”))
可除的除以=int(原始输入(“序列中除:”)的所有数字之和)
#以下内容与“a,b=0,1”相同
a=0
b=1
“”“为上面的变量提供了单字母变量,以便在下面的公式中使用
(较长的字符串似乎不起作用)”
c=结束数
d=可除以
fib_list=[]
#当b小于或等于结束数时,它将循环。

而b生成器使用的内存比列表生成器少

def fibNum(n):
    a,b = 1,1
    while b < n:
        yield b
        b,a = a+b,b
n = int(raw_input("Enter the end number here "))
d = int(raw_input("Sum all numbers in the sequence that divide by: ")) 
total = sum(i for i in fibNum(n) if i%d == 0)
print ("Sum of all natural numbers that divide by the number you chose") 
print ("and are less than the other number you chose is: %d" %total)
def fibNum(n):
a、 b=1,1
而b
这非常有效。很高兴看到一个简单的添加来让它工作!