【问题标题】:python--rounding value of expression--unexpected EOFpython--表达式的舍入值--意外的EOF
【发布时间】:2013-09-30 04:08:32
【问题描述】:

在最后一行中,我试图将输出四舍五入到小数点后一位,但是当我测试这个时,我得到了一个意外的 EOF 错误。请帮忙?

count = 0
scoreSum = 0
score = 0
while score != 999:
    score = float(input("Enter test score or enter 999 to finish: "))
    if score > 0 and score < 100:
        scoreSum += score
        count += 1
    elif (score <0 or score > 100) and score != 999:
        print("This score is invalid, Enter 0-100")
else:
    print ("your average is: ", round((scoreSum / count), 2)

【问题讨论】:

  • 这太简单了,你在一分钟内得到了 3 个完全相同的答案 :)。向我们提出一些更好的问题!我承认 Python 的错误信息没有什么帮助。
  • 哈哈,我只是个初学者,给点时间吧。

标签: python rounding


【解决方案1】:

最后一行应该是:

print ("your average is: ", round((scoreSum / count), 2))

你漏掉了一个右括号。

完整的错误信息可能是'unexpected EOF while parsing',这确实有点神秘。专业提示:以后如果你发现一些你不理解的错误,总是past the complete error message directly into Google。在大多数情况下,其他人already asked about the same problem

【讨论】:

    【解决方案2】:

    最后一行是你的问题——你需要一个右括号:

    print ("your average is: ", round((scoreSum / count), 2))
    #                                            right here ^
    

    实际上,你可以让你的代码行是这样的:

    print("your average is: ", round(scoreSum / count, 2))
    

    不需要那些额外的括号。

    【讨论】:

      【解决方案3】:

      您在结尾处缺少右括号:

      print ("your average is: ", round((scoreSum / count), 2))
                                                              ^ THIS
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多