【问题标题】:Something wrong with this file program?这个文件程序有问题吗?
【发布时间】:2015-11-15 12:29:39
【问题描述】:

我有一个这样的程序:

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_rate = 600,但是当我运行程序时,它就像总金额=0。要么什么都没有,要么 total_rate=0 来。声明有什么问题吗??

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

【问题讨论】:

  • 你在哪里计算,为什么你有函数嵌套在一个函数中?
  • 实际上真正的程序很长,所以我缩短了它以获得主要思想。我有嵌套函数作为程序的一部分。
  • 我可以建议使用类结构而不是嵌套函数吗?让我知道我会分享一个例子。
  • 我已经有一个庞大的类程序,这些程序将以该类中的函数的形式出现。你能帮我解决这个问题吗,因为这是我计划的一部分。感谢您的努力!
  • 好的,我有一个解决方案。但是因为你不希望我不会分享它。让我们分解问题。你可以放弃外部函数 def facility():.如果不放弃,您将无法访问 rates5/vrates/funrate 变量。因为它们在函数的本地范围内。

标签: python file record rate


【解决方案1】:

让我知道这种方法是否适合您:

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

【讨论】:

  • 我忘记了全局关键字!!!。非常感谢。有效。我理解了这个错误。感谢您的时间和努力!
  • 我们也可以使用嵌套函数,但是对于每个你必须使用的本地函数:facility.variable 名称。但这更容易。谢谢。如果您有时间,请也发布您的课程解决方案。谢谢!
  • 完成。让我知道这是否太多。会清理干净的。
  • 这对我很有帮助。谢谢。我从来没有想过这个。如果你不介意,你能看看我的另一个问题:“这个python程序中的类协助??”我也有点卡在那里。
  • 一个链接会更好。 :) 链接:stackoverflow.com/questions/33712227/…
【解决方案2】:

您的程序存在许多问题:

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

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

【讨论】:

  • 非常抱歉遗漏了通话声明。
  • 我列表中的第 4 页仍然是实际的 - 变量未分配(如果 total_rate=rates5+vrates+funratefacilities() 正文的一部分是正确的)
  • 你如何解决它?我在调用语句之前输入,在它们之后输入。尽可能但仍然无法正常工作。我真的不明白这个问题。请帮助。
  • 您有total_rate=rates5+vrates+funrate,但您之前从未定义过rates5vratesfunratefacilities 函数中有同名的变量,但在全局层面上它们与这些名称无关。
  • 谢谢你,@Matthias 你是对的。 @vanikavaisya-trust,您必须将这些变量定义为全局变量(请参阅相应的 python 文档)——但这是一种糟糕的风格方法;或在适当的函数中正确初始化 var 并使用 return 语句返回所需的值
猜你喜欢
  • 2011-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多