In: Computer Science
I've updated this as much as I can think of. This is a PYTHON problem that is due today. Please help. I'm trying to read names from a file that begin with @ and print them to another file. It will also read multiple lines of sales amounts for each name and write them and then total them with the end being ***END*** at the bottom of each person's figures and total. I am supposed to write a try and except to insert as well if the numbers aren't actual numbers. Trying to make it a float or produce an error message.
def main():
PRE = '@'
SEP = '***END***\n'
inputFile = open(\r'C:\Users\Trey\Desktop\Programming\good.txt',
'r')
outputFile =
open(\r'C:\Users\Trey\Desktop\Programming\goodTrey.txt', 'w')
for line in inputFile:
amount = 0
if inputFile.startswith(PRE):
outputfile.write(' ',line)
elif:
amount = float(line)
outputfile.write('$'{:,.2f}.format(amount),'\n')
else:
inputFile.startswith(SEP):
total += amount
outputfile.write('$'{:,.2f}.format(total),'\n',
'***END***\n')
# close the files
inputFile.close()
outputFile.close()
#call the main function
main()
The following errors can be found in the given program:
possible fixes:
program:
def main():
PRE = '@'
SEP = '***END***\n'
inputFile = open(r'C:/Users/Trey/Desktop/Programming/good.txt',
'r' 'r')
outputFile =
open(r'C:/Users/Trey/Desktop/Programming/goodTrey.txt', 'w')
total = 0
for line in inputFile.readlines():
amount = 0
if line[0]==PRE:
outputFile.write(line)
elif line == SEP:
outputFile.write('${:,.2f}'.format(total)+'\n***END***\n')
total = 0
else:
try:
amount = float(line)
total += amount
outputFile.write('${:,.2f}'.format(amount)+'\n')
except:
print("Invalid entry at", line)
# close the files
inputFile.close()
outputFile.close()
#call the main function
main()
sample intput and output:
@person1
235.23
23.44
2345.23
***END***