【发布时间】: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 变量。因为它们在函数的本地范围内。