Write a python program that reads the contents of the charge_accounts.txt file i
ID: 3687688 • Letter: W
Question
Write a python program that reads the contents of the charge_accounts.txt file into a list. The program should then ask the user to enter a charge account number, each account number is 7 digits long. The program should determine whether the number is valid by searching for it in the list. If the number is in the list, the program should display a message indicating the number is valid. If the number is not in the list, the program should display a message indicating the number is invalid.
The program should have the following functions:
main function - controls the overall flow of the program.
getList function - reads the charge_accounts. txt file into a list and returns the list.
See a sample program execution below:
Explanation / Answer
def main():
try:
# Get the possible charge account number
possAccountNumber = int(input('Please enter a charge account nubmer: '))
# Open the file of charge account numbers and read them into a list while stripping off the and making it an int
validNumbers = open('charge_accounts.txt', 'r')
validList = []
for accountNumber in validNumbers:
validList.append(int(accountNumber.rstrip(' ')))
# Search for the possAccountNumber in the validList
if possAccountNumber not in validList:
print('The account number you are looking for does not exist. Please try another.')
else:
print('You have entered a valid account number.')
# Close the account number file
validNumbers.close()
# Except error where a string is entered.
except ValueError as err:
print('You have entered an invalid account number.')
print('The error message is displayed below.')
print(err)
except IOError as err:
print('The charge_accounts.txt file cannot be located.')
print('Please verify that it exists and is named correctly')
except Exception as err:
print('An unknown error has occured.')
print(err)
main()
charge_accounts.txt
5658845
4520125
7895122
8777541
8451277
1302850
8080152
4562555
5552012
5050552
7825877
1250255
1005231
6545231
3852085
7576651
7881200
4581002
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.