这个文件程序有问题吗?
Something wrong with this file program?
我有一个这样的程序:
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 来了。声明有问题吗??
实际上真正的程序很长所以我缩短了它以了解主要思想。我有嵌套函数作为程序的一部分。
请帮助!
你的程序有很多问题:
facilities()
函数没有 return 任何值
- 函数
food()
、recreation()
和 transport()
已声明但从未调用
rates5
、funrate
和 vrates
适当函数内的变量在内部作用域中定义。结果永远不会 returned
rates5
、funrate
和 total_rate=rates5+vrates+funrate
中的 vrates
未分配
此外,我很感兴趣你是如何运行成功获得结果的
让我知道这种方法是否适合您:
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 解决方案:
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
我有一个这样的程序:
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 来了。声明有问题吗??
实际上真正的程序很长所以我缩短了它以了解主要思想。我有嵌套函数作为程序的一部分。 请帮助!
你的程序有很多问题:
facilities()
函数没有 return 任何值- 函数
food()
、recreation()
和transport()
已声明但从未调用 rates5
、funrate
和vrates
适当函数内的变量在内部作用域中定义。结果永远不会 returnedrates5
、funrate
和total_rate=rates5+vrates+funrate
中的vrates
未分配
此外,我很感兴趣你是如何运行成功获得结果的
让我知道这种方法是否适合您:
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 解决方案:
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