首页 > 解决方案 > 语法错误:非 UTF-8?

问题描述

age = int(input("Please type your age:"))
if int(age) >= 18:
    print("Sorry, you couldn't enter!")
else:
    print("Congratulation, have a good time!")

SyntaxError: 第 1 行的文件 go to.py 中以 '\xa3' 开头的非 UTF-8 代码,但未声明编码;

为什么?

标签: pythonutf-8

解决方案


您的代码包含几个 python 解释器无法识别的字符。它们看起来像普通(的并且:

In [8]: ord('(')
Out[8]: 40

In [9]: ord('(')
Out[9]: 65288

In [10]: ord(':')
Out[10]: 58

In [11]: ord(':')
Out[11]: 65306

似乎您从某处复制了代码 - 只需手动将所有括号和冒号替换为有效字符


推荐阅读