3 Replies - 1437 Views - Last Post: 04 April 2012 - 08:37 PM Rate Topic: -----

#1 4jzbb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-March 12

Value returning functions and file input/output

Posted 04 April 2012 - 10:27 AM

Hello,

I'm having problem to get my code to work correctly.... Can anyone give me any ideas about where I am going wrong?
Thanks!

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:
19273
48923
0

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 ================================
>>>
water used:
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>
main()
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: ''
>>>

Is This A Good Question/Topic? 0
  • +

Replies To: Value returning functions and file input/output

#2 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Value returning functions and file input/output

Posted 04 April 2012 - 10:53 AM

Your error is on this line:

w_used = round(int(water_usage)/100)


As you could probably see from the error message. It is saying that water_usage isn't a value that can be converted in to an integer.

The problem is that when you read a line from FinalProjectBill.txt, you have opened it to read and straight after opened it to write, so the value of water_usage will be empty (and not a decimal number), the file will be overwritten.

A better idea is to do all the writing after the reading if you are going to use the same file, or create different files for input and output.
Was This Post Helpful? 0
  • +
  • -

#3 4jzbb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-March 12

Re: Value returning functions and file input/output

Posted 04 April 2012 - 08:11 PM

Thank you for your reply. How do I " do all the writing after the reading if you are going to use the same file, or create different files for input and output"?.
Was This Post Helpful? 0
  • +
  • -

#4 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: Value returning functions and file input/output

Posted 04 April 2012 - 08:37 PM

Take these steps:

  • Open the file in read mode
  • Read to variable 1
  • Read to variable 2
  • ... read to variable n
  • Close the file
  • Perform any calculations
  • Open it again in write mode
  • Write any variables necessary to file
  • Write to the file
  • Close the file


If you want different files for the input and output, you can keep the code as you have, but change the names of the relevant files.

Hope that helps :)

This post has been edited by Simown: 04 April 2012 - 08:38 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1