简体   繁体   English

python 3.6中的缩进错误

[英]Indentation Error in python 3.6

When i run this code in IDLE, i get a SyntaError(expected an indent block) The thing is that i dont see any indentation problem. 当我在IDLE中运行此代码时,出现SyntaError(预期为缩进块)问题是我看不到任何缩进问题。 What the code does: It reads 'cities.txt' file(with string lines) It then copies those lines in 'out.txt', after enumeration in each line 代码的作用:读取“ cities.txt”文件(带有字符串行),然后在每一行进行枚举后将这些行复制到“ out.txt”中

try:
    with open('cities.txt', 'r+') as inp:
except FileNotFoundError:
    print('File not found')
except:
    print('Error')
else:
    try:
        with open('out.txt', 'a+') as out:
    except FileNotFoundError:
        print('File not found')
    except:
        print('Error')
    else:
        for i, line in enumerate(inp):
            out.write(str(i+1)+': '+line)
            print(str(i+1)+': '+line, end='\n')

with x as y:应该紧跟一个缩进的代码块,该代码块可以完成您想要使用的资源-基本上是您在else块中拥有的

To ensure logic and format, you should write your code like this : 为了确保逻辑和格式,您应该这样编写代码:

try:
    with open('out.txt', 'a+') as out:
        for i, line in enumerate(inp):
            out.write(str(i+1)+': '+line)
            print(str(i+1)+': '+line, end='\n')
except FileNotFoundError:
    print('File not found')
except:
    print('Error')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM