Reputation: 13
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. 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. 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).
Edit: It also doesn't work if the while loop is set to 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
Upvotes: 0
Views: 2922
Reputation: 9745
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).
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.break
if you want to leave the loop and keep the indexSo 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.")
Upvotes: 1
Reputation: 405755
A while loop will keep looping as long as its condition is True
.
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
. That makes the entire condition True
, since you're using or
logic.
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]
). Check the length instead (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.)
Upvotes: 1