无法打印字符串,不断出现无法调用的错误

时间:2013-12-04 21:27:14

标签: python

所以这就是代码:

def variousScores(students):
    best = 0
    worst = 100
    average = 0
    count = 0
    for s in students:
        if s.examScore > best:
            best = s.examScore
        if s.examScore < worst:
            worst = s.examScore
        average = s.examScore + average
        count = count + 1
    average = average/count
    return best, worst, average

def printScores(best, worst, average, stndrdDev):
    print("\n\nWorst score:        {:4.2f}.format"(worst))

我一直在TypeError: 'str' object is not callableprint("\n\nWorst score: {:4.2f}.format"(worst)) 我刚刚开始上课,我觉得这就是搞砸了我。 有什么建议吗?

1 个答案:

答案 0 :(得分:4)

您当前的代码尝试将字符串"\n\nWorst score: {:4.2f}.format"作为函数调用,因为您在其后放置了(worst)

最后一行应该写成:

print("\n\nWorst score:        {:4.2f}".format(worst))

请注意,.format部分位于格式字符串的外部上。这是因为str.format是一个字符串方法。