2016-04-23 43 views
0

好的 - 我试图让Python函数接受来自另外两个函数的变量。这可能吗 ?Python:1函数可以接受来自2个不同函数的变量

我正在尝试做下面的一个样本(我已经将原始代码模拟下来 - 在此处输入)。希望你能理解我想要做的事情。简而言之,我有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") 

回答

2

rectangle,你叫extras(),那么你只发送功能extrascalculate_deposit()。你想发送extras()调用的结果,而不是对函数本身的引用。您可以进行小的更改并保存该值,在打印时以及何时进入calculate_deposit时参考该值。

更改此:

print output + extras() 
total_cost = calculate_deposit(output, extras) 

要这样:

extra = extras() 
print output + extra 
total_cost = calculate_deposit(output, extra) 
相关问题