在以下代码块中:
dictionary = dict()
dictionary[0] = {}
try:
print(dictionary[0]["tomato"])
except Exception as e:
print(e) # prints 'tomato'
print(str(e)) # prints 'tomato'
if str(e) == 'tomato':
print("Not tomato") # never prints, why?即使print(e)打印'tomato',str(e) == 'tomato'也不是True
谁能解释一下这是怎么回事?
发布于 2020-12-02 18:18:51
前两个print语句打印'tomato',包括单引号,这意味着引号实际上是字符串的一部分。
将if语句更改为:
if str(e) == "'tomato'":https://stackoverflow.com/questions/65113810
复制相似问题