2013-02-04 126 views
-5

这是我的代码:蟒蛇 - 错误类型

def get_num(): 
    number = int(raw_input(" ")), 
    print "|" 
    return number 
list_of_letter = ['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'] 
num = -1 
print "welcome to the sudoko maker!!!" 
print "I will start asking you information." 
for lines in range (9): 
    for ask in range(9): 
     if num == 25: 
      num = 0 
     num = num + 1    
     print list_of_letter [num], 
    print 

,当我运行它的Python写: IndexError:列表索引超出范围 为什么????

+0

不要浪费问号! – Paolo

+2

@Guandalino:嘿,不要这么快判断。也许Python 3.4将'IndexError'的默认错误描述从''list index out of range''改为''list index out of range why'''。 :) – abarnert

+1

@Roey,我不是一个答案,但仍然可以帮助:对于这类问题考虑使用称为[pdb]的Python模块(http://docs.python.org/2/library/pdb.html )。这是调试器(基本上你会使用'pdb.set_trace()'和一些键,比如l,n,c,s等等)。这是一个非常教学,自助和不太复杂的工具。如果您仍然遇到问题,请在分享您的发现的问题中添加详细信息。也许一些downvote也会消失。 – Paolo

回答

2

您缺少list_of_letter列表中的几个逗号;没有从元素'k'开始的逗号。其结果是,list_of_letter没有25个项目,但只有11个

+3

是的,这是问题,但你仍然需要解释_why_他得到这个错误:他有第11个“字母”'klmnopqrstuvwxyz'',之后没有更多,因为相邻的字符串是连接的。 – abarnert

+3

无需编写所有这些字符。只需使用'string.ascii_lowercase'。 – Matthias

+1

+1 Matthias。如果你真的想要一个'list'(你可能不需要,因为'ascii_lowercase [3]'和'list_of_letters [3]'是一样的''),你可以使用'list(string。 ascii_lowercase)'。 – abarnert

1

除了塔马斯答案,它会更容易阅读和作用是相同的,如果你做list_of_letter一个字符串(可以被索引以同样的方式):

list_of_letter = 'abcdefghijklmnopqrstuvwxyz' 
+1

这与'string.ascii_lowercase'是一样的,但更容易出错。 – abarnert

+0

@abarnert很酷,很高兴知道 – mVChr