TypeError: 'str' object is not callable in generator python

TypeError: 'str' object is not callable in generator python

def gen_secs():
    x = 0
    while x < 60:
        yield x
        x += 1

def gen_minutes():
    x = 0
    while x < 60:
        yield x
        x += 1

def gen_hours():
    x = 0
    while x < 24:
        yield x
        x += 1

def gen_time():
    for x in gen_hours():
        for y in gen_minutes():
            for z in gen_secs():
                yield ("%d:%d:%d" (x, y, z))

for gt in gen_time():
    print(gt)
    if gt == "01:23:45":
        break

函数gen_time不能return一些字符串参数 原因。有语法问题,我找不到问题。

您忘记了 format str 语法,请尝试更改以下内容:

yield ("%d:%d:%d" (x, y, z))

至:

yield ("%d:%d:%d" % (x, y, z))

This 这篇文章很有帮助