2016-04-23 10 views
0

OK - 他の2つの関数から変数を受け入れるPython関数を取得しようとしています。これは可能ですか?Python:1つの関数が2つの異なる関数から変数を受け入れることができます

私が下にしようとしているもののサンプル(私はここに入力するために元のコードをシミュレートしました)。うまくいけば、私がやろうとしていることのうちにあなたが得られるはずです。簡単に言えば、Extras()を呼び出すRectangle()があり、RectangleとExtrasの出力をCalculate_Deposit()に送信する必要があります。

これは可能ですか? rectangle

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

、あなたは、あなたがcalculate_deposit()にだけ機能extrasを送って、extras()を呼び出します。 extras()呼び出しの結果を送信し、関数自体への参照ではありません。マイナーチェンジを行い、その値を保存することができます。印刷するときと、calculate_depositに入るときに参照してください。

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

これに:この

変更

extra = extras() 
print output + extra 
total_cost = calculate_deposit(output, extra) 
関連する問題