You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

我的Python多边形对角线计算项目出现语法错误,请求排查

多边形对角线计算代码的SyntaxError错误修复

问题背景

这是一个用于计算多边形对角线数量的作业代码,运行时触发语法错误,原代码如下:

z=int(input("The number of sides:"))

if z<3:

    print("ERROR")

else:

    w=z*(z-3)//2

    print("Number of diameters:" ,w)

错误信息(中文翻译)

Traceback (most recent call last):
  File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
    start(fakepyfile,mainpyfile)
  File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
    exec(open(mainpyfile).read(),  __main__.__dict__)
  File "<string>", line 3
        print("ERROR")
    ^
SyntaxError: 无效的不可打印字符U+00A0
[程序结束]

错误核心提示:SyntaxError: invalid non-printable character U+00A0(语法错误:存在无效的不可打印字符U+00A0)

问题原因

U+00A0是不间断空格(非普通空格),Python对缩进规则严格,仅允许使用普通空格(U+0020)或制表符(Tab)作为缩进标识,无法识别这类特殊空格,因此触发语法错误。你的代码中print("ERROR")和else代码块内的缩进使用了该特殊空格。

修复方案

  1. 替换特殊缩进空格:将代码中异常缩进的U+00A0替换为普通空格,或直接用Tab键重新完成缩进。
  2. 修正术语错误:原代码中Number of diameters用词错误,多边形的“对角线”英文应为diagonals,而非diameters(直径)。

修复后的代码:

z = int(input("The number of sides:"))

if z < 3:
    print("ERROR")
else:
    w = z * (z - 3) // 2
    print("Number of diagonals:", w)

内容的提问来源于stack exchange,提问作者Chegal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 19:35:21