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

3.14通

我正在尝试使用变量来查找所有斐波那契,并能够将它们汇总起来(如果可以通过所选变量进行除法)。我也想尝试不使用任何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

我可以运行代码,但是得到的答案错误。例如,如果我运行代码以将所有斐波纳契数加起来等于21并被3整除,则得到的答案为“ 0”。答案应该是“ 24 =(3 + 21)”。

一个简单的修改的任何帮助将是巨大的!

斯卡姆西

您需要先将斐波那契数字添加到列表中,以便稍后在进行除法检查时使用它们。其次,您需要将总数从循环中取出,因为正如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

此代码还将考虑您提供的最后一个数字,例如,如果您希望的数字是21和3,则总数将为24(21和3除以3)。但这不是您的印刷品所说的“并且小于您选择的其他数字” ...因此,您应考虑将其更改为“小于或等于”

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在Haskell中使用List构造计算fib-速度差异

来自分类Dev

说明以下JavaScript语法:var fib = require('algorithms.js')。Math.fibonacci;

来自分类Dev

如何使递归 fib 函数通过记忆返回正确的值

来自分类Dev

TypeError:无法在Fib中将'int'对象转换为隐式str

来自分类Dev

斐波那契递归,为什么 fib(2) 打印两个?

来自分类Dev

python中的循环和嵌套导入

来自分类Dev

python中的循环和嵌套导入

来自分类Dev

在python中更改while和for循环

来自分类Dev

在python中循环导入

来自分类Dev

在python的url中使用While和For循环

来自分类Dev

如何遍历python中来自sql存储过程的表。使用While循环和Pyodbc?

来自分类Dev

按降序打印字典中的键和值,无需导入运算符(Python)

来自分类Dev

sql服务器中数字的数字总和,而无需使用while等传统循环

来自分类Dev

在 Python 中循环导入字典

来自分类Dev

结合使用readline()和for循环,而无需在python中使用next()

来自分类Dev

while循环python中的while循环

来自分类Dev

while循环python中的while循环

来自分类Dev

python中的while循环内的while循环?

来自分类Dev

比较和计数python中的项目(while循环)

来自分类Dev

在Python中同步Linux系统命令和while循环

来自分类Dev

Python 3 中的 while 循环和 True/False 条件

来自分类Dev

列表的和while循环-Python

来自分类Dev

在Python中多次使用“和”循环

来自分类Dev

使用最小和最大条件的While循环Python

来自分类Dev

在Python中使用while和for循环遍历列表-问题

来自分类Dev

如何在python中使用while循环和范围?

来自分类Dev

使用while循环和if条件

来自分类Dev

使用while循环和变量

来自分类Dev

使用while循环来迭代python 3中的类成员

Related 相关文章

热门标签

归档