首页 > 解决方案 > 不明白为什么这段代码会引发 EOF 错误

问题描述

我正在尝试在 Windows 10 上创建一个循环遍历 Unicode 字符的 Python 3 程序,但在最后一个空行上,它的错误为

SyntaxError: unexpected EOF while parsing

我试着把 aprint("Done!")放在最后,但那是 IndentationError: unexpected unindent.

max = int("FFFC",16)
min = 0
for x in range(max + 1):
    try:
        hex_value = hex(x)
        proper = str(hex_value)[2:].upper()
        while len(proper) != 4:
            proper = "0" + proper
        proper = "U+" + str(proper)
        print(f"{proper} : {chr(x)}")

错误:

    File "unicode.py", line 11
                                       ^
SyntaxError: unexpected EOF while parsing

这是代码的最后一行(空白处)。

标签: pythonpython-3.x

解决方案


您需要except为每个try.


推荐阅读