如果不使用yield,这该怎么写呢?

发布于 2025-01-15 06:39:29 字数 1016 浏览 5 评论 0原文

我曾多次尝试替换yield,但都失败了,但我似乎没有成功,目标是拥有相同的功能,但没有yield。

def calcPi(limit):  # Generator function
    """
    Prints out the digits of PI
    until it reaches the given limit
    """

    q = 1
    r = 0
    t = 1
    k = 1
    n = 3
    l = 3

    decimal = limit
    counter = 0

    while counter != decimal + 1:
        if 4 * q + r - t < n * t:
            # yield digit
            yield n
            # insert period after first digit
            if counter == 0:
                yield '.'
            # end
            if decimal == counter:
                print('')
                break
            counter += 1
            nr = 10 * (r - n * t)
            n = ((10 * (3 * q + r)) // t) - 10 * n
            q *= 10
            r = nr
        else:
            nr = (2 * q + r) * l
            nn = (q * (7 * k) + 2 + (r * l)) // (t * l)
            q *= k
            t *= l
            l += 2
            k += 1
            n = nn
            r = nr

添加了人们在评论中要求的完整功能,

I've tried and failed multiple times trying to replace yield, but I don't seem to be successful, the goal is to have the same function, without yield.

def calcPi(limit):  # Generator function
    """
    Prints out the digits of PI
    until it reaches the given limit
    """

    q = 1
    r = 0
    t = 1
    k = 1
    n = 3
    l = 3

    decimal = limit
    counter = 0

    while counter != decimal + 1:
        if 4 * q + r - t < n * t:
            # yield digit
            yield n
            # insert period after first digit
            if counter == 0:
                yield '.'
            # end
            if decimal == counter:
                print('')
                break
            counter += 1
            nr = 10 * (r - n * t)
            n = ((10 * (3 * q + r)) // t) - 10 * n
            q *= 10
            r = nr
        else:
            nr = (2 * q + r) * l
            nn = (q * (7 * k) + 2 + (r * l)) // (t * l)
            q *= k
            t *= l
            l += 2
            k += 1
            n = nn
            r = nr

Added the full function as people were asking for it in the comments,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

dawn曙光 2025-01-22 06:39:29

只需定义一个列表并收集数字:

def calcPi(limit):  # Generator function
    """
    Collects the digits of PI
    until it reaches the given limit
    """
    pi = []
    ...

    while ...:
        ...
        # yield n
        pi.append(n)
        ...
            #break
            return pi

Just define a list and collect the digits:

def calcPi(limit):  # Generator function
    """
    Collects the digits of PI
    until it reaches the given limit
    """
    pi = []
    ...

    while ...:
        ...
        # yield n
        pi.append(n)
        ...
            #break
            return pi
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文