我正在为bot编写一个数学插件,并在python交互式shell中测试我的代码,该shell正常执行:
>>> text = "!math 0.023*67"
>>> string1 = [b for b in a for a in text.split("!math ") if len(a) != 0]
>>> print string1
['0', '.', '0', '2', '3', '*', '6', '7']
但当我将其包含在脚本中时,它会失败,并出现一个类型错误:
Traceback (most recent call last):
File "/Users/ema/Openshift/pythonbot/plugins/math/math.py", line 61, in <module>
string1 = [b for b in a for a in text.split("!math ") if len(a) != 0]
NameError: name 'a' is not defined
你发布的代码也不起作用。在交互式解释器中执行
del a
,然后再次尝试运行它。你会看到它失败了:你把循环的顺序搞混了。从左到右按嵌套顺序列出:
现在不需要预先定义
a
即可工作:相关问题 更多 >
编程相关推荐