2016-04-23 70 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) 
相關問題