Question

In: Computer Science

I've updated this as much as I can think of. This is a PYTHON problem that...

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()

Solutions

Expert Solution

The following errors can be found in the given program:

  • eilf expects a condition, there is none given.
  • there are few string formating errors in outputFile.write() calls.
  • it is a good practice to get the list of lines from inputFile using readlines() method.
  • there are few typos.

possible fixes:

  • change the loop to : for line in inputFile.readlines()
  • format the string arguments in outputFile.write() . strings are concatenated with + operator not , operator.
  • switch the else and elif part and add a condition to elif to check for SEP
  • add try except block while reading floats.
  • fix the typos

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:

  • we used the following file in this program:

@person1
235.23
23.44
2345.23
***END***

  • we got the following output file


Related Solutions

I need a python program and a flowchart please!! Problem #1:    How much should I...
I need a python program and a flowchart please!! Problem #1:    How much should I study outside of class?                         Issue: Your fellow students need help. This is their first year in college and they need to determine how many hours they need to study to get good grades. Study Hours Per Week Per Class                    Grade 15                                                           A 12                                                           B 9                                                             C 6                                                             D 0                                                             F Project Specifications: The user enters their full name and the number of...
"How can I connect to Hadoop Hive using Python? I've tried using PyHive but always get...
"How can I connect to Hadoop Hive using Python? I've tried using PyHive but always get the error: ...could not start SASL... no mechanism available: unable to find a callback: 2"
I cannot figure out this problem, I've made a few attempts at solving it. I could...
I cannot figure out this problem, I've made a few attempts at solving it. I could really use some help. The question is : 75.00 mL of 0.225 M HNO2 is titrated to its equivalence point with 0.100 M NaOH. What is the pH at its' equivalence point? Please help!
Ok, I've stumbled onto what I think is a bit of a paradox. First off, say...
Ok, I've stumbled onto what I think is a bit of a paradox. First off, say you had some computer in a very fast(near light speed) centrifuge. You provide power to this computer via a metal plate on the "wall" of the centrifuge's container, so it works similar to how subways and streetcars are powered. If the computer normally would consume 200 watts, how much power would it consume at say 1/2 of light speed? Would it consume 400 watts...
I am a student taking python programming. Can this problem be modified using the define main...
I am a student taking python programming. Can this problem be modified using the define main method, def main()? #Define showExspenses function def showExpenses(loan,insure,gas,oil,tyres,maintenance): expense=loan+insure+gas+oil+tyres+maintenance #Print monthly and yearly automobile operating expenses print("\nTotal Monthly expenses for operating expenses you entered = ",expense) print(f"\nTotal Yearly expenses for operating expenses you entered = 12 *",expense,f"= {expense*12:,}") #yearly expenses loan=int(input("Enter Loan :")) insure=int(input("Enter Insurance :")) gas=int(input("Enter Gas :")) oil=int(input("Enter Oil :")) tyres=int(input("Enter Tyres :")) maintenance=int(input("Enter Maintenance:")) showExpenses(loan,insure,gas,oil,tyres,maintenance)
I am posting this problem because I've had trouble following the steps listed in similar problems....
I am posting this problem because I've had trouble following the steps listed in similar problems. Please don't answer if you are only going to copy/paste a version of those same steps; It will not be helpful. When a suspected drunk driver blows 186 mL of his breath through this breathalyzer, the breathalyzer produces an average of 323 mA of current for 10 s. Assuming a pressure of 1.0 atm and a temperature of 24 ∘C, what percent (by volume)...
This is a question about coding in python. I think that the question is asking me...
This is a question about coding in python. I think that the question is asking me to code the following: Implement the scalar product of 2 vectors in 2-space, using two tuple parameters (the function scalarProduct2). Write a docstring; then write some test function calls (can I pick any data for this? - the question gave these examples data examples: (1., 1.) dot (2,3) = (1.,1.) dot (2,0) = (1., 1.) dot (0,2) = (1., 1.,) dot (4,5) = );...
Can I get this logic in Python? plus what software I can use it to run?...
Can I get this logic in Python? plus what software I can use it to run? thanks and will rate!!! Search Lab implement the following logic in Python, use appropriate data types. Data types are represented as either numeric (num) or string. string name string address num item num quantity num price num SIZE = 6 num VALID_ITEM [SIZE] = 106, 108, 307, 405, 457, 688 num VALID_ITEM_PRICE [SIZE] = 0.59, 0.99, 4.50, 15.99, 17.50, 39.00 num sub string foundIt...
How can I prepare the context diagram, DFD and flowchart for payroll system? I've tried few...
How can I prepare the context diagram, DFD and flowchart for payroll system? I've tried few times but I have been asked to redo it again. Can anyone please explain or show me the correct way ?
Python: I am not sure where to begin with this question, and I hope I can...
Python: I am not sure where to begin with this question, and I hope I can get an input on it. This is in regards to Downey's program, and we are asked to make two changes. Downey prints time as they do in the Army: 17:30:00 hours. We want to print that as 5:30 PM. Downey lets you define the time 25:00:00 - we want to turn over at 23:59:59 to 00:00:00. (I am asked to identify my changes with...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT