2014-01-07 30 views
0
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()函數的一個條件,但我得到IndexError:列表索引超出範圍。我知道else語句導致它,但我很困惑,因爲python不應該執行它。IndexError在未使用條件

完整代碼

#!/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() 
+0

列表是0索引,他們從0到len() - 1。 – uselpa

+0

索引從0開始。你在尋找'if pos == len(menulist)-1'嗎? – freakish

回答

2

有很多如果pos != len(menulist)值其中menulist[pos+1]給出IndexError(包括pos == len(menulist) - 1)。您應該檢查

if pos > (len(menulist) - 2): 
+0

謝謝你解決了我的問題,出界了 – afiolmahon

0

你或許應該改變if pos == len(menulist):if pos == len(menulist) - 1:,如果你想檢查是否pos是最後一個元素的索引,或者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方式。也許你可以告訴我們你想要達到的目標,這裏的某個人可能會提出一個更好的方法。

+0

謝謝,這解決了我的問題! – afiolmahon

+0

@afiolmahon然後,你應該考慮upvoting它.. :) hehe –