2014-09-24 132 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