这个文件程序出了什么问题?

时间:2015-11-15 12:29:39

标签: python file record rate

我有一个这样的程序:

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

最后我想计算funrate,vrates和rates5.eg:funrate = 100,vrates = 200,rates5 = 300,然后total_rate = 600,但是当我运行程序时,它就像总计金额= 0。无论是什么都没有,或者total_rate = 0来了。宣言有什么问题吗?

实际上真正的程序很长,所以我缩短了它以获得主要想法。我有嵌套函数作为程序的一部分。  请帮忙!

2 个答案:

答案 0 :(得分:0)

您的计划存在许多问题:

  • facilities()函数不返回任何值
  • 声明了函数food()recreation()transport(),但从未调用
  • 内部范围中定义了适当函数中的
  • rates5funratevrates变量。结果永远不会返回
  • rates5funratevrates total_rate=rates5+vrates+funrate未分配

此外,我对你如何成功运行它以获得任何结果感兴趣

答案 1 :(得分:0)

如果这种方法适合您,请告诉我:

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

另一种方法是:

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

针对该问题的结构化类解决方案:

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