2013-10-30 47 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()调用将会非常生气

相关问题