我正在尝试构建一个程序,该程序将为用户给出的每个输入的每个输入输出一个参数名称,直到并包括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()
请帮助...谢谢。 =)
正确格式化你的代码 –
'while district_number> = len(senator):''replace while'district_number> len(senators):' – Goat
'当你得到索引错误 – jramirez