2017-07-15 8 views
2

totalprint(result)関数が私の結果を出力していません。関数の戻り値の出力

sums関数は、結果値を呼び出す関数に結果値を返してはなりませんか?

これは私のコードです:

def main(): 

    #Get the user's age and user's best friend's age. 

    firstAge = int(input("Enter your age: ")) 
    secondAge = int(input("Enter your best friend's age: ")) 
    total(firstAge,secondAge) 

def total(firstAge,secondAge): 
    sums(firstAge,secondAge) 
    print(result) 

#The sum function accepts two integers arguments and returns the sum of those arguments as an integer. 

def sums(num1,num2): 
    result = int(num1+num2) 
    return result 

main() 

私は、Python-3.6.1を使用しています。

+1

'result = sums(firstAge、secondAge)' – janos

答えて

5

結果は返されますが、何も割り当てられません。したがって、結果変数は、印刷しようとすると定義されず、エラーが発生します。

はあなたの合計関数を調整し、sums関数の範囲で定義された変数resultの差により明確にするために、この場合responseで、変数に戻り合計値を割り当てます。変数に変数を割り当てたら、変数を使用して変数を出力できます。

def total(firstAge,secondAge): 
    response = sums(firstAge,secondAge) 
    print(response) 
+0

ありがとう。私は価値を持ち変数に割り当てると思われていた気がしましたが、どうしたらよいかわかりませんでした。 – Cornel