【问题标题】:Indexing error with variables变量索引错误
【发布时间】:2014-09-29 00:35:45
【问题描述】:
#Dancing Text :D
listinput = raw_input("Input text(usually between 1-10 letters ")
dance_time = raw_input("Alternate How Many Times? ")
listinput = list(listinput)
length = len(listinput)
alternate = 0
counter = 0
first_input = listinput
swap_counter = 0
tempchar = ""
counter = int(counter) #Just to make sure...getting errors

#above are just defining variable types and changing types to lists.

while counter <= length:
    if alternate == 0: #first, third, fifth character
        alternate = alternate + 1
        counter = counter + 1
    elif alternate == 1: #second, fourth, sixth character and so on....
        tempchar = listinput[counter]
        tempchar = str(tempchar)
        tempchar = tempchar.swapcase()
        listinput[counter] = tempchar #It should be the number 1 the first time it runs, but it gives a index error.
        alternate = alternate - 1
        counter = counter + 1

while swap_counter <= dance_time:
    print first_input
    print input
    swap_counter = swap_counter + 2

这是错误-

第 20 行,在模块中

tempchar = listinput[计数器]

IndexError: 列表索引超出范围

我查找了索引错误,'1'应该在列表的范围内,(我每次测试时都会输入'hello')

如果我理解正确的话,第 20 行,当它运行时应该是“listinput[1]”,变量 'counter' 是 1,因为它只循环了一次。

【问题讨论】:

    标签: python indexing


    【解决方案1】:

    我认为您的问题是您假设列表的索引以 1 开头。实际上它们以 0 开头。因此,如果您的列表中有一项,则 list[0] 将为您提供该项。

    所以,要能够执行

    tempchar = listinput[counter]
    

    你必须确定

    counter < length
    

    是真的,这里不是这样,因为计数器可以等于长度。

    【讨论】:

    • 谢谢!我知道索引从 0 开始,我只是忘记在执行
    【解决方案2】:

    您的dance_time 是一个字符串。请使用swap_counter &lt;= int(dance_time):

    如果有帮助的话,请说几句:

    您不需要临时字符。可以转换代码:

        tempchar = listinput[counter]
        tempchar = str(tempchar)
        tempchar = tempchar.swapcase()
        listinput[counter] = tempchar 
    

    到单行:

        listinput[counter] = str(listinput[counter]),swapcase()
    

    请注意,您只是根据alternate 的值交换交替字符的大小写。我猜想虽然在这个例子中设置为0,但在一个真实的程序中,你会想要随意改变它。然后,您可以尝试以下方法:

    for i, l in enumerate(listinput):
        if   alternate == 0:  
            if i%2 == 1: # odd samples
                listinput[i] = str(listinput[i]).swapcase()
        elif alternate == 1:  
            if i%2 == 0: # even samples
                listinput[i] = str(listinput[i]).swapcase()
        else:
            pass 
    

    或者简单地说:

    for i, l in enumerate(listinput):
        if (alternate+i)%2 == 1: # odd samples
            listinput[i] = str(listinput[i]).swapcase()
    

    这是同样的事情,假设alterante01,这可以很容易地用if 条件检查。

    最后,请注意 Python 中的函数是一等值。因此,您也可以返回一个函数,并将其用作条件,如下所示:

    def evenOdd(alternate):
        if   alternate == 0:  return lambda x: x%2 == 1
        elif alternate == 1:  return lambda x: x%2 == 0
    
    print [  (l.swapcase() if evenOdd(alternate)(i) else l) for i, l in enumerate(listinput) ]
    

    这更容易阅读......

    在这里,我让事情变得简单。如果alternate 不是01,则您可能希望在evenOdd 函数中使用raise ValueError

    最后,按照上面的例子,你也可以这样做:

    [(l.swapcase() if (i+alternate)%2==0 else l) for i, l in enumerate(listinput)]
    

    【讨论】:

    • 谢谢,我后来意识到了。
    猜你喜欢
    • 2015-12-26
    • 2016-06-23
    • 1970-01-01
    • 1970-01-01
    • 2015-08-27
    • 2021-01-04
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    相关资源
    最近更新 更多