【问题标题】:Unexpected EOF error in python [duplicate]python中出现意外的EOF错误[重复]
【发布时间】:2015-05-20 14:02:28
【问题描述】:

我刚学python,不明白为什么下面的简单代码不起作用:

while True:
    try:
        print "Counter 1: %(counter)"
        counter += 1

        if counter > 15:
            break

【问题讨论】:

  • 您的try 不需要exceptfinally 块吗?

标签: python python-2.7 eof


【解决方案1】:

您缺少try 表达式的关闭。所以:

while True:
    print ("Counter %s": %(counter))
    counter += 1

    if counter > 15:
        break

会更好

但如果你学习,请查看以下解决方案:

for i in range(0, 15):
    print ("Counter %s": %(counter))

【讨论】:

    【解决方案2】:

    如果你使用try:,你需要有except子句:

    while True:
        try:
            print "Counter 1: %(counter)"
            counter += 1
    
            if counter > 15:
                break
        except StandardError:
            print 'Foo'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多