Search code examples
pythonfilerecordrate

Something wrong with this file program?


i have a program like this:

def facilities():
     total_rate=vrates=funrate=rates5=0
     def food():
           #files are used
           rates5=0
           # calculate the rate and store the total rate in rates5
     def recreation():
            #files are used
             funrate=0
            # calculate the rate and store the total rate in funrate
     def transport():
           #files are used
           vrates=0
           # calculate the rate and store the total rate in vrates
     food()
     recreation()
     transport()
total_rate=rates5+vrates+funrate
print"The total amount=",total_rate
facilities() 

at the end i want to calculate the sum of funrate,vrates and rates5.eg: funrate=100,vrates=200,rates5=300,then total_rate=600,but when i run the program, it comes like The total amount =0. either nothing comes or total_rate=0 comes. is there anything wrong with the declaration??

actually the real program is very long so i have shortened it to get the main idea. i have nested functions as that ia part of the program. please help!


Solution

  • Let me know if this approach works for you:

    total_rate = 0
    vrates = 0
    funrate = 0
    rates5 = 0
    
    
    def food():
        global rates5
        # files are used
        rates5 = 1
        # calculate the rate and store the total rate in rates5
    
    
    def recreation():
        global funrate
        # files are used
        funrate = 2
        # calculate the rate and store the total rate in funrate
    
    
    def transport():
        global vrates
        # files are used
        vrates = 3
        # calculate the rate and store the total rate in vrates
    
    food()
    recreation()
    transport()
    total_rate = rates5 + vrates + funrate
    print"The total amount =", total_rate
    

    another approach is this :

    total_rate = 0
    vrates = 0
    funrate = 0
    rates5 = 0
    
    def facilities():
        global total_rate, vrates, funrate, rates5
    
        def food():
            global rates5
            # files are used
            rates5 = 1
            # calculate the rate and store the total rate in rates5
    
        def recreation():
            global total_rate, vrates, funrate, rates5
            # files are used
            funrate = 2
            # calculate the rate and store the total rate in funrate
    
        def transport():
            global total_rate, vrates, funrate, rates5
            # files are used
            vrates = 3
            # calculate the rate and store the total rate in vrates
    
        food()
        recreation()
        transport()
    
    facilities()
    
    total_rate=rates5 + vrates + funrate
    print "The total amount=", total_rate
    

    And a structured class solution for the problem:

    class Facilities(object):
        def __init__(self):
            self.total_rate = 0
            self.vrates = 0
            self.funrate = 0
            self.rates5 = 0
    
        def food(self):
            # files are used
            self.rates5 = 1
            # calculate the rate and store the total rate in rates5
            return self.rates5
    
        def recreation(self):
            # files are used
            self.funrate = 2
            # calculate the rate and store the total rate in funrate
            return self.funrate
    
        def transport(self):
            # files are used
            self.vrates = 3
            # calculate the rate and store the total rate in vrates
            return self.vrates
    
        def get_total_rate(self):
            return self.food() + self.recreation() + self.transport()
    
    facilities_obj = Facilities()
    total_rate = facilities_obj.food() + facilities_obj.recreation() + facilities_obj.transport()
    # another option
    total_rate = facilities_obj.get_total_rate()
    print "The total amount =", total_rate