Question: If you have downloaded the source code from the Premium Companion Website you will find a file named charge_accounts.txt in the Chapter 07 folder. This

If you have downloaded the source code from the Premium Companion Website you will find a file named charge_accounts.txt in the Chapter 07 folder. This file has a list of a company’s valid charge account numbers. Each account number is a seven-digit number, such as 5658845.

Write a program that reads the contents of the file into a list. The program should then ask the user to enter a charge account number. 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.

(You can access the Premium Companion Website at www.pearsonglobaleditions.com/gaddis.)


Well, this is my code, but when I run it in IDLE it comes back as a syntax error----expected an indented block and Invalid syntax

def main():

try:

file=open('charge_accounts.txt', 'r')

acc_num=file.readlines()

starting_index=0

while(starting_index!=len(acc_num)):

acc_num[starting_index]=

int(acc_num[starting_index])

starting_index +=1

search=int(input('Enter Account Number: '))

if(search in acc_num):

print(search, ' :Yes, the account number is VALID')

else:

print(search, ':No, the account number is INVALID')

file.close()

except ValueError:

print('Cannot open the file')

main()

This is the charge_accounts.txt

5658845

4520125

7895122

8777541

8451277

1302850

8080152

4562555

5552012

5050552

7825877

1250255

1005231

6545231

3852085

7576651

7881200

4581002

Step by Step Solution

3.36 Rating (152 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

It looks like there are some syntax errors and issues in your code Let me help you fix them def main ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Starting Out With Python Questions!