未使用的条件中的IndexError

时间:2014-01-07 23:27:07

标签: python list if-statement conditional

def menurender():
global pos
global menulist
line1=menulist[pos]
if pos == len(menulist):
    line2="back"
else:   
    line2=menulist[pos+1]

lcd.clear()
lcd.message(str(pos)+' ' +line1+ "\n"+str(pos+1)+' '+ line2)

在我的代码块中,我在menurender()函数中有一个条件,它检查以确保列表menulist在引用它之前有一个有效的索引,但我收到IndexError:list index超出范围。我理解else语句导致它,但我很困惑,因为python不应该执行它。

完整代码

    #!/usr/bin/python

#################################################
#IMPORTS#########################################
#################################################
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate
from Adafruit_I2C import Adafruit_I2C

#################################################
#OBJECTS#########################################
#################################################
lcd = Adafruit_CharLCDPlate()


#################################################
#VARIABLES#######################################
#################################################
#current button value
prevbutton = "NULL"
#for SELECT key and determining clicks
action = False
#variable for menu position
pos = 0
#on screen cursor 0 for top line, 1 for bottom line
cursor = 0

#Handles list structure and action when clicked
menulist= []
menulist.append("CPU")
menulist.append("RAM")
menulist.append("STORAGE")
menulist.append("NETWORK")


#get input from keys and return the currently pressed key
def buttonstatus():
    bstatus = "Null"
    if lcd.buttonPressed(lcd.SELECT) == True:
        bstatus="SELECT"
    elif lcd.buttonPressed(lcd.UP) == True:
        bstatus="UP"
    elif lcd.buttonPressed(lcd.DOWN) == True:
        bstatus="DOWN"
    elif lcd.buttonPressed(lcd.LEFT) == True:
        bstatus="LEFT"
    elif lcd.buttonPressed(lcd.RIGHT) == True:
        bstatus="RIGHT"
    return bstatus

#checks buttons pressed and converts that into action for top menu

def getinput():
    global prevbutton
    global pos
    if buttonstatus() != prevbutton:
        prevbutton = buttonstatus()
        if buttonstatus() == "SELECT":
            print "select"
        elif buttonstatus() == "DOWN":
            pos = pos + 1
        elif buttonstatus() == "UP":
            pos = pos -1
        #elif buttonstatus() == "LEFT":
            #print "left"
        #elif buttonstatus() == "RIGHT":
            #print "right"

#defines bounds for the position of the cursor
def posbounds():
    global pos
    global menulist
    if pos < 0:
        pos = 0
    if pos == len(menulist):
        pos = len(menulist)


#code renders the menu on the LCD
def menurender():
    global pos
    global menulist
    line1=menulist[pos]
    if pos == len(menulist):
        line2="back"
    else:   
        line2=menulist[pos+1]

    lcd.clear()
    lcd.message(str(pos)+' ' +line1+ "\n"+str(pos+1)+' '+ line2)


while True:
    getinput()
    posbounds()
    menurender()

2 个答案:

答案 0 :(得分:2)

pos != len(menulist)menulist[pos+1]提供IndexError(包括pos == len(menulist) - 1)的if pos > (len(menulist) - 2): 值有很多。你应该检查

{{1}}

答案 1 :(得分:0)

如果要检查pos是否是最后一个元素的索引,则应该将if pos == len(menulist):更改为if pos == len(menulist) - 1:,如果要检查倒数第二个元素,则应更改为if pos == len(menulist) - 2:元件。

更好的方法是使用try ... except块。

try:
    line2 = menulist[pos+1]
except IndexError:
    # Out of range -> pos is greater than len(menulist)-1
    line2 = 'back'

然而 - 在Python中,这似乎并不是Pythonic做任何事情的方式。也许你可以告诉我们你想要达到的目标,这里有人可能会提出一个更好的方法。