2013-02-04 45 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