2013-10-30 86 views
0

我正在嘗試構建一個程序,該程序將爲用戶給出的每個輸入的每個輸入輸出一個參數名稱,直到幷包括31個參數。我有一個參數列表,每個參數都屬於一個特定數字,當用戶輸入一個數字結果是相應的參議員姓名。 當我運行該程序,不過,我不斷收到錯誤消息爲什麼我得到一個List索引超出範圍?

"Index Error: List index out of range". 

我在做什麼錯?這裏是代碼:

def main(): 
    senators = ['Kevin Eltife', 'Bob Deuell','Robert Nichols', 'Tommy Williams', 
       'Charles Schwertner', 'Sylvia Garcia', 'Dan Patrick', 'Ken Paxton', 
       'Kelly Hancock', 'Wendy Davis', 'Larry Taylor', 'Jane Nelson', 
       'Rodney Ellis', 'Kirk Watson', 'John Whitmire', 'John Carona', 
       'Joan Huffman', 'Glenn Hegar', 'Carlos Uresti', 'Juan "Chuy" Hinojosa', 
       'Judith Zaffirini', 'Brian Birdwell', 'Royce West', 'Troy Fraser', 
       'Donna Campbell', 'Leticia Van de Putte', 'Eddie Lucio, Jr.', 
       'Robert Cuncan', 'Jose Rodriguez', 'Craig Estes', 'Kel Seliger'] 

    district_number = int(input('Give the senator''s district number (enter 0 to end): ')) 
    while district_number > len(senators): 
     print('That district number does not occur in Texas.') 
     district_number = int(input('Enter another valid district number (enter 0 to quit): ')) 

    while district_number != 0: 
     print('That district is served by the honorable ', senators[district_number - 1], '.', sep='') 
     district_number = int(input('Enter another district number (enter 0 to quit): ')) 

# Call the main function. 
main() 

請幫助...謝謝。 =)

+0

正確格式化你的代碼 –

+0

'while district_number> = len(senator):''replace while'district_number> len(senators):' – Goat

+0

'當你得到索引錯誤 – jramirez

回答

0

那是因爲你直接使用索引號:

print('That district is served by the honorable ', senators[district_number - 1], '.', sep='') 

你應該把這種圍繞一個try/except塊:

while district_number != 0: 
    try: 
     print('That district is served by the honorable ', senators[district_number - 1], '.', sep='') 
    except IndexError: 
     print("District Number too high") 
    district_number = int(input('Enter another district number (enter 0 to quit): ')) 

控制檯會話:

Give the senators district number (enter 0 to end): 22 
That district is served by the honorable Brian Birdwell. 
Enter another district number (enter 0 to quit): 100 
District Number too high 
Enter another district number (enter 0 to quit): 
+1

您不通過添加try/catch塊來解決不正確的編碼問題,您可以修復代碼。 – summerbulb

+0

感謝遊戲Brainiac。這真的解決了獅身人面像的謎。現在我看到了我的錯誤。 –

+0

@FredRamirez那麼,如果這爲你工作,請upvote並接受答案。 –

2

一旦你得到的第一個輸入少於31,你進入一個循環,你不再檢查最大的區域噸數。所以一個序列說20和40會導致索引錯誤。

1

首先,我建議不要使用列表。如果你使用字典,你可以使用get()方法,並簡單地檢查它是否返回結果。雖然我不是專業人士,但這似乎是最佳做法。這是一個例子。

SENATORS = {1: 'Kevin Eltife', 
      2: 'Bob Deuell', 
      3: 'Robert Nichols', 
      4: 'Tommy Williams', 
      5: 'Charles Schwertner', 
      6: 'Sylvia Garcia', 
      7: 'Dan Patrick'} 

while True: # loop until program is exited 
    district_number = int(input('Give the senator''s district number (enter 0 to end): ')) 
    senator = SENATORS.get(district_number) # Will return None if user enters invalid number 
    if senator: 
    print('That district is served by the honorable ', senator, '.', sep='') 
    else: 
    print('That district number does not occur in Texas.') 

在我看來,這不僅更容易閱讀,但會更容易在未來,你應該需要進行編輯,並消除用戶錯誤(除非他們當然型「鴨」或「棒球」,在這種情況下,你的int()調用將會非常生氣

相關問題