好的 - 我试图让Python函数接受来自其他两个函数的变量。这可能吗?
我想在下面尝试做的一个示例(我已经简化了原始代码 - 这里输入)。希望你能得到我想做的事情。简而言之,我有Rectangle()调用Extras(),我想将Rectangle和Extras的输出发送到Calculate_Deposit()。
这可能吗?
def calculate_deposit(total_cost, extras):
deposit_percent = float(raw_input("Enter Deposit % (as a decimal) of Total Cost: "))
months_duration = float(raw_input("Enter the number of months client requires: "))
if deposit_percent >0:
IN HERE JUST SOME CALCULATIONS
else:
print "The total amount required is: ", total_cost
def rectangle(width, height, depth, thickness):
type = raw_input("Enter lowercase c for concrete: ")
if type == 'c':
output = IN HERE JUST COME CALCULATIONS
else:
return raw_input("Oops!, something went wrong")
print output + extras()
total_cost = calculate_deposit(output, extras)
def extras():
type = float(raw_input("Enter 1 for lights: "))
if type == 1:
light = 200
print "The cost of lights are: ", light
return light
else:
return raw_input("No extras entered")
答案 0 :(得分:2)
在rectangle
,您拨打extras()
,然后只将功能extras
发送给calculate_deposit()
。您想发送extras()
调用的结果,而不是对函数本身的引用。您可以进行微小的更改并保存该值,在您打印时以及进入calculate_deposit
时引用它。
改变这个:
print output + extras()
total_cost = calculate_deposit(output, extras)
对此:
extra = extras()
print output + extra
total_cost = calculate_deposit(output, extra)