python - 关于斐波那契数列生成器的问题 - 没有打印任何内容

标签 python fibonacci

我在类里面被分配了一个作业,我需要编写一个可以生成“n”个斐波那契数列的算法。 我觉得这段代码应该可以工作,但我仍然无法找到阻止它工作的错误。 该算法应该从数字 2 开始打印斐波那契数列(前 3 个数字已修补),但它只打印前 3 个数字,而我的算法不打印任何内容。 我什至进行了复杂的错误检查,应该排除错误的值,但它也没有报告任何内容。

#/usr/bin/python

import re
import math
import time
import sys

def errchk(hwmny,int1,int2,int3,int4,errcnt):
    #Sanity check; first 12 numbers don't have 6's
    expression = re.compile(r"^[^6]{1,3}", re.I | re.S) #expression for no sixes
    if expression.match(str(int3)):     # if it doesn't match
        return                      # return
    else:                           #otherwise
        if (hwmny > 12):        # as long as it's less than 12
            return          # no errors
        zints.err()         #but ifnot, errors
    return None

def nofloats():
    return math.floor(zints.int3)

class zeroints:
    int3=int4 = 0   #int3 and int4 are 0
    errcnt = 0  #count the number of errors
    def err(self):
        print "Errors"
        return

hwmny =15       #hwmny variable is 10
int1 =int2 =1       #int1 and int2 are 1
zints = zeroints()  #ints init'd to 0
print "0\n1\n1"

def loop(hwmny,int1,int2,int3,int4,errcnt):
    while(((zints.int4<hwmny)or not(zints.int4>0))and not(zints.int4==0)):  #while int4 is less than hwmny and error checking
        zints.int3 =int1+ int2          #int3 is int1 plus int2
        int1 =int2              #int1 is int2
        string = 'The Values Are'       #string is set to 'The Values Are'
        int2= zints.int3            #int2 is int3
        errchk(hwmny,int1,int2,zints.int3,zints.int4,zints.errcnt)
        print zints.int3            #print int3
        zints.int4= zints.int4+1        #int4 is int4 plus 1
        loop(hwmny,int1,int2,zints.int3,zints.int4,zints.errcnt)
        continue

loop(hwmny,int1,int2,zints.int3,zints.int4,zints.errcnt)

最佳答案

这个修复看起来很简单。 while循环中的分组是错误的,将其更改为以下内容应该可以正常工作:

while((zints.int4<hwmny)or not(zints.int4>0)and not(zints.int4==0)):

关于python - 关于斐波那契数列生成器的问题 - 没有打印任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53421986/

相关文章:

Python 单元测试 : Mock problematic module/function from another Module

python - Rabbitmq 一个队列多个消费者

java - 在 Java 中使用 BigInteger 的递归斐波那契数列

java - 如何将相同的单元测试应用于不同的功能

javascript - 如何在 javaScript 中获得所有奇数斐波那契值的总和?

Python - 迭代传递给函数的参数

python - 查找数组是否为排列的最佳方法

Python正则表达式检查字符串是否包含方法调用

functional-programming - 斐波那契与内存 Elixir

r - 在 R 中计算斐波那契数的结果不准确