[英]list index out of range for while loop
def main():
infile = open('charge_accounts.txt', 'r')
chargeAccounts = infile.readlines()
index = 0
while index < len(chargeAccounts):
chargeAccounts[index] = chargeAccounts[index].rstrip('\n')
index += 1
userAccount = input("Please enter a charge account number: ")
count = 0
while userAccount != chargeAccounts[count] or chargeAccounts[count] == chargeAccounts[17]:
count += 1
if chargeAccounts[index] == userAccount:
print("The account number", userAccount, "is in the list.")
else:
print("The account number", userAccount, "in not in the list.")
main()
I'm trying to make a code in python that has the user input a number and checks to see if that number is in a list. 我正在尝试用python创建一个代码,让用户输入一个数字并检查该数字是否在列表中。 When I try to run this code, I get an error that says that the list index is out of range in the while loop.
当我尝试运行此代码时,出现一条错误,指出列表索引在while循环中超出范围。 There are only 18 items in the list, and I have the while loop set to terminate at 17(which should be 18 in the list).
列表中只有18个项目,我将while循环设置为终止于17(在列表中应为18)。 Edit: It also doesn't work if the while loop is set to
chargeAccounts[count] != chargeAccounts[17]:
编辑:如果while循环设置为
chargeAccounts[count] != chargeAccounts[17]:
它也将不起作用chargeAccounts[count] != chargeAccounts[17]:
Here's the exact error code: 这是确切的错误代码:
Traceback (most recent call last):
File "F:/CPT 168/Ch 7/AndrewBroughton_Chapter7_Excersizes/7-5/7-5.py", line 23, in <module>
main()
File "F:/CPT 168/Ch 7/AndrewBroughton_Chapter7_Excersizes/7-5/7-5.py", line 13, in main
while userAccount != chargeAccounts[count] or chargeAccounts[count] != chargeAccounts[17]:
IndexError: list index out of range
Here's the content of the original text file: 这是原始文本文件的内容:
5658845
4520125
7895122
8777541
8451277
1302850
8080152
4562555
5552012
5050552
7825877
1250255
1005231
6545231
3852085
7576651
7881200
4581002
A while loop will keep looping as long as its condition is True
. 只要条件为
True
while循环将保持循环。
count = 0
while userAccount != chargeAccounts[count] or chargeAccounts[count] == chargeAccounts[17]:
count += 1
If I enter an invalid userAccount
to your program, the first part of the condition userAccount != chargeAccounts[count]
is always going to be True
. 如果我向您的程序输入了无效的
userAccount
,则条件userAccount != chargeAccounts[count]
的第一部分将始终为True
。 That makes the entire condition True
, since you're using or
logic. 这使整个条件为
True
,因为您正在使用or
逻辑。
Furthermore, if you want to check to see if you've reached the end of a list, you don't have to check the contents of the last list element ( chargeAccounts[count] == chargeAccounts[17]
). 此外,如果要检查是否已到达列表末尾,则不必检查最后一个列表元素的内容(
chargeAccounts[count] == chargeAccounts[17]
)。 Check the length instead ( count == len(chargeAccounts)
). 而是检查长度(
count == len(chargeAccounts)
)。
To fix this, change that loop condition to something like 要解决此问题,请将循环条件更改为类似
while count < len(chargeAccounts) and userAccount != chargeAccounts[count]:
(I'm not sure if this is exactly what you need, because I don't really follow the logic of your program. This should get you past the current error, though.) (我不确定这是否正是您所需要的,因为我并不真正遵循程序的逻辑。不过,这应该可以使您摆脱当前的错误。)
You get the error at the string if chargeAccounts[index] == userAccount:
because index
is already bigger than the index of the last element of the list (because you left the loop with this index
above). if chargeAccounts[index] == userAccount:
在字符串处出现错误,因为index
已经大于列表最后一个元素的索引(因为您使循环离开了上面的该index
)。
I would recommend you to follow a few rules to work with lists that could save you from similar errors with indices. 我建议您遵循一些规则来使用列表,这样可以避免索引出现类似错误。
for
-loop instead of while
-loop, if you lineary go through each element. for
-loop,而不是while
-loop,如果你lineary经过的每个元素。 break
if you want to leave the loop and keep the index break
So you code may look like this: 因此,您的代码可能如下所示:
with open('charge_accounts.txt', 'r') as infile:
chargeAccounts = infile.readlines()
for index in range(len(chargeAccounts)):
chargeAccounts[index] = chargeAccounts[index].strip()
userAccount = input("Please enter a charge account number: ")
found = False
for chargeAccount in chargeAccounts:
if chargeAccount = userAccount:
found = True
break
if found:
print("The account number", userAccount, "is in the list.")
else:
print("The account number", userAccount, "in not in the list.")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.