即使在ab行上有一个直接的、无条件的赋值,名称也会出错

2024-04-19 15:06:37 发布

您现在位置:Python中文网/ 问答频道 /正文

告诉我怎么可能:

сhar = input()
print(char)

Traceback (most recent call last): File "test.py", line 2, in print(char) NameError: name 'char' is not defined

为了让事情更有趣,可以考虑在复制使用双重检查的python版本:

import sys
print(f'Python version on this machine:\n{sys.version}')
сhar = input()
print(char) 

Python version on this machine:
3.7.4 (default, Jul 13 2019, 14:20:24)
[GCC 6.3.0 20170516]
type anything
Traceback (most recent call last):
File "main.py", line 5, in
print(char)
NameError: name 'char' is not defined


Tags: inpymostinputversionlinecallfile
2条回答

你的char = input()包含西里尔字符С(参见here) 而print(char)是纯拉丁语。你知道吗

这也让我想起了greek question mark prank,其中分号和问号的字符呈现几乎相同:;;

编辑:wim更快

Unicode标识符名称。你知道吗

>>> "char" == "сhar" 
False

其中一个c是普通的拉丁小写字母c,而另一个是chr(0x441),即'CYRILLIC SMALL LETTER ES'。在一台典型的机器上,它们将以非常相似(或完全相同)的glyph呈现给终端。你知道吗

相关问题 更多 >