Python 名称错误
list1 = [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z]
for item in list1:
print item
NameError: "name 'a' is not defined"
我不太明白为什么上面的代码会出现这个错误:
7 个回答
2
字符串字面量应该用引号括起来 :)
list1 = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
7
你需要把字符串放在(双)引号里
list1 = ["a","b","c",...]
应该可以正常工作
12
除了正确使用引号之外,不要重新输入字母表。
>>> import string
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> L = list(string.ascii_lowercase)
>>> print L
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', ...
>>> help(string)