2014-09-24 67 views
-1

我正在嘗試做一個hang子手遊戲,你可以在其中輸入一個特定的詞。列表索引超出範圍-python

當試圖製作一個代碼來計算單詞中相同字母的數量時,我遇到了一個問題。例如,'taste'將有兩個t s。

程序會計算將用於數學和的字母,以便字母可以輸入兩次,但不再輸入。

我在下面的代碼中遇到列表索引超出範圍的問題。

def nolettercheck(): 

    global list45 #list of variable within code (so they can be accessed) 
    global list1 
    global countonce 
    global count0 
    global count1 
    global count2 
    global count3 
    global count4 
    global count5 
    global count6 
    global count7 
    global count8 
    global count9 
    if len(list1) == 1: #makes sure list is in range 
     if list1.count(list1[0]) > 1: """count how many letter in word and if above 1 it continues 
             (although it doesn't work perfectly yet BTW do not spoil 
              this answer for me unless 
             you have to).""" 
      count0 = list1.count(list1[0])#create variable for number of same letters 
      list45.insert(0,list1[0])#insert the letter into another list for comparison 
    if len(list1) == 2 or len(list1) < 2: """repeats with next letter until 
              similar statement =false""" 
     if list1.count(list1[1]) > 1: 
      count1 = list1.count(list1[1]) 
      list45.insert(1,list1[1]) 
    if len(list1) == 3 or len(list1) < 3: 
     if list1.count(list1[2]) > 1: 
      count2 = list1.count(list1[2]) 
      list45.insert(2,list1[2]) 
    if len(list1) == 4 or len(list1) < 4: 
     if list1.count(list1[3]) > 1: 
      count3 = list1.count(list1[3]) 
      list45.insert(3,list1[3]) 
    if len(list1) == 5 or len(list1) < 5: 
     if list1.count(list1[4]) > 1: 
      count4 = list1.count(list1[4]) 
      list45.insert(4,list1[4]) 
    if len(list1) == 6 or len(list1) < 6: 
     if list1.count(list1[5]) > 1: 
      count5 = list1.count(list1[5]) 
      list45.insert(5,list1[5]) 
    if len(list1) == 7 or len(list1) < 7: 
     if list1.count(list1[6]) > 1: 
      count6 = list1.count(list1[6]) 
      list45.insert(6,list1[6]) 

代碼繼續,所以它可以做到最多10個字母的單詞。這段代碼假設將no的字母存儲到一個變量中(儘管我可能會進一步改變它,但它會變成列表後面的列表)。它也應該存儲這封信(如果有多個字母)。

該錯誤特別關注與if list1.count(list[6]) > 1:類似的行。

我之前遇到過這個錯誤,並且if len(list1) == 7 or len(list1) <7:正常工作(因爲它停止檢查列表中的虛數值)。所以我對此很困惑。

+2

哎喲,這很痛苦。你應該考慮製作10個計數列表,而不是10個單獨的變量,一個循環覆蓋範圍(1,8),而不是複製和粘貼相同的代碼7次,並且最可能使用比列表更合適的數據結構你必須繼續調用'count'和'insert' ... – abarnert 2014-09-24 18:49:49

+0

而且,len(list1)== 7或len(list1)<7'可以寫成len(list1)<= 7'。 – abarnert 2014-09-24 18:50:55

+0

你能否詳細闡述一下你試圖從中獲得什麼?根據我的理解,你只是想從一個單詞中計算一個字母(但限制爲2?)。我相信會有一個更簡單的方法來做到這一點,因爲你在這裏看起來效率非常低。 – 2014-09-24 18:52:13

回答

0

這是一種獲取單詞字母計數字典的方法。這樣做有更多'pythonic'方法,但如果你剛開始,這可能會幫助你。

a = 'somestring' 
letter_count_dict = {} 
for letter in set(a): 
    letter_count_dict[letter] = a.count(letter) 

在單詞的不同字母

set(a) #set takes the distinct values. If you want a list of distinct values, an easy way to do it is list(set(a)) but since sets are iterable you don't usually need to do this. 

,並給出一個字母,L,它的出現次數爲

letter_count_dict[l] #if l='s', then this would output 2 

一旦你有計數,可以選擇如何處理它們......循環找到發生的事件不止一次,等等。祝你好運。

+0

謝謝!我希望避免爲每封信製作一本完整的字典,但它現在可能稍微有效一些。 – 2014-09-24 20:26:12