0

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!

  • where are you calculating and why do you have functions nested inside a function? – Padraic Cunningham Nov 15 '15 at 12:37
  • 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. – vanikavaisya trust Nov 15 '15 at 12:40
  • May i suggest using a class structure instead of nested function? Let me know i will share an example. – Vikash Singh Nov 15 '15 at 13:04
  • i already have a huge class program and these are to be in the form of functions inside that class. could you help me with this because that is part of my plan. Thank you for your effort! – vanikavaisya trust Nov 15 '15 at 13:10
  • Ok, i have a class solution for this. But as you dont want that i wont share it. Lets break the problem down. you can ditch the external function def facilities():. Without ditching that you cannot access rates5/vrates/funrate variables. as they are in the local scope of the function. – Vikash Singh Nov 15 '15 at 13:16
  • @vanikavaisyatrust : can you check the solution below? – Vikash Singh Nov 15 '15 at 13:25

2 Answers2

0

There are numerous issues with your program:

  • facilities() function doesn't return any value
  • functions food(), recreation() and transport() declared but never called
  • rates5, funrate and vrates vars inside appropriate functions are defined in inner scope. The results are never returned
  • rates5, funrate and vrates in total_rate=rates5+vrates+funrate are unassigned

Besides, I'm interested how you've run it successfully to get any result

Dmitry
  • 432
  • 2
  • 6
  • i am extremely sorry to have left out the call statemnts. – vanikavaisya trust Nov 15 '15 at 12:46
  • still p.4 from my list still is actual - variables are unassigned (if it's correct that `total_rate=rates5+vrates+funrate` is part of `facilities()` body) – Dmitry Nov 15 '15 at 12:54
  • how do you fix it?? i typed before call statements,after them.everywhere possible but still not working. i sincerely dont understand the problem.please help. – vanikavaisya trust Nov 15 '15 at 12:56
  • You have `total_rate=rates5+vrates+funrate` but you never defined `rates5`, `vrates` and `funrate` before. There are variables with the same name in the `facilities` function, but they have nothing to do with these names on a global level. – Matthias Nov 15 '15 at 13:09
  • Thank you, @Matthias You are right. @vanikavaisya-trust, you have to define these variables as global ones (see appropriate python docs) - but it's a bad style approach; or correctly initialize vars inside of appropriate functions and return required values with `return` statement – Dmitry Nov 15 '15 at 13:18
  • If its not causing trouble , can you fix it without the function facilities?? Thank you – vanikavaisya trust Nov 15 '15 at 13:20
0

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
Vikash Singh
  • 13,213
  • 8
  • 40
  • 70