这个文件程序有问题吗?

2024-10-03 19:24:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样一个程序:

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的和。例如:funrate=100,vrates=200,rates5=300,然后total\u rate=600,但是当我运行程序时,它就像总量=0。要么什么都没有,要么total\u rate=0。申报有什么问题吗

实际上,真正的程序是非常长的,所以我缩短了它得到的主要思想。我有嵌套函数作为程序的一部分。 请帮帮我


Tags: andthestorein程序ratedeffiles
2条回答

如果这种方法对您有效,请告诉我:

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

您的程序有许多问题:

  • facilities()函数不返回任何值
  • 函数food()recreation()transport()声明但从未调用
  • ^相应函数中的{}、funratevrates变量在内部作用域中定义。结果永远不会返回
  • ^total_rate=rates5+vrates+funrate中的{}、funratevrates未赋值

另外,我很感兴趣的是你是如何成功地运行它来获得任何结果的

相关问题 更多 >