I'm having problem to get my code to work correctly.... Can anyone give me any ideas about where I am going wrong?
This is the assignment:
The City of Lalatown needs a program to calculate water and sewage bills for the residents it serves. The charge for water in Lalatown is 1.2 cents per 100 gallons used.
Only whole hundred gallons are charged for. For example the cost of 121 gallons, 175 gallons, and 199 gallons is all the same cost as 100 gallons. The charge for sewage is 1.0 cents per 100 gallons of water used. Again sewage charges are based only on whole hundred gallons. Both water and sewage charges are rounded to the nearest cent.
The city tax is 2% of the total sewage and water charges and is rounded to the nearest cent.
Input: The input file will contain integers one per line representing the number of gallons of water used. Input will be terminated by a value of 0.
The input file will be named FinalProjectBill.txt, you will need to create your own for testing.
Output: For each amount write the bill to a file for the amount of water used. The bill will include the charge for water, for sewage, for city tax and the total of the bill. Your output should be formatted like the example (include a line, simply a string of underscores, between each bill).
Name your output file with your name and project - example JohnDoeFinal.txt
Example: Suppose that the input file contained the following:
Your resulting output file should look similar to this:
Water Used: 48923
Water Charge: 5 dollars 87 cents
Sewage charge: 4 dollars 89 cents
City Tax: 0 dollars 22 cents
Total Bill: 10 dollars 98 cents
Water Used: 19273 gallons
Water Charge: 2 dollars 30 cents
Sewage charge: 1 dollars 92 cents
City Tax: 0 dollars 8 cents
Total Bill: 4 dollars 30 cents
This is my code:
# This program uses the records that are # in the FinalProjectBill.txt file def main(): # Open employees.txt file. emp_file = open('FinalProjectBill.txt', 'r') # Open a file for writing. out_file = open('FinalProjectBill.txt', 'w') # Read the first line from the file, which is # the water_usage field of the first record. water_usage = emp_file.readline() # If a field was read, continue processing. while water_usage != '0': # Strip the newlines from the fields. water_usage = water_usage.rstrip('\n') print('water used:', water_usage) print w_used = round(int(water_usage)/100) water_charge = w_used * .012 sewer_charge = w_used * .01 sub_total = water_charge + sewer_charge city_tax = sub_total * .02 total = sub_total + city_tax print('Water charge:', format(water_charge, ',.2f')) print('Sewer charge:', format(sewer_charge, ',.2f')) print('City tax:', format(city_tax, ',.2f')) print('Total Bill:', format(total, ',.2f')) print('__________________________________') out_file.write(water_usage+'\n') out_file.write(format(water_charge, ',.2f')+'\n') out_file.write(format(sewer_charge, ',.2f')+'\n') out_file.write(format(city_tax, ',.2f')+'\n') out_file.write(format(total, ',.2f')+'\n') print('The water usage is: ') #Read the water_usage field of the nest record. water_usage = emp_file.readline() #Close the file. emp_file.close() out_file.close() # Call the main function. main()
Below is the feedback from Python:
Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
Traceback (most recent call last):
File "C:/Users/owner/Desktop/Spring 2012/CIS 115 Intro to Prog and Logic/Unit 6/Water_Supply_Problem.py", line 50, in <module>
File "C:/Users/owner/Desktop/Spring 2012/CIS 115 Intro to Prog and Logic/Unit 6/Water_Supply_Problem.py", line 22, in main
w_used = round(int(water_usage)/100)
ValueError: invalid literal for int() with base 10: ''