2016-06-16 4 views
-2

좋아요, 그래서 주 함수에 4 함수를 호출 할 때 약간의 문제가 있습니다. 나는 1 시간 이상 여기에 앉아 있었고 그것을 이해할 수 없다.주요 문제에 여러 함수 호출

나는이 오류가 계속 : 당신의 기능에 로컬 변수를 생성 할 이유가 없다,

(total) = total(num, num2) UnboundLocalError: local variable 'total' referenced before assignment

def main(): 

    num = float(input(" Enter a number: ")) 
    num2 = float(input(" Enter another number: ")) 

    (total) = total(num, num2) 
    (diff) = difference(num, num2) 
    (product) = product(num, num2) 
    (quot) = quotient(num, num2) 

    print(" The total is: ", format(total, ".1f"), sep="") 
    print(" The difference is: ", format(diff, ".1f"), sep="") 
    print(" The product is: ", format(product, ".2f"), sep="") 
    print(" The quotient is: ", format(quot, ".2f"), sep="") 

def total(num, num2): 
    total = (num + num2) 
    return(total) 


def difference(num, num2): 
    diff = abs(num - num2) 
    return(diff) 


def product(num, num2): 
    product = num * num2 
    return(product) 


def quotient(num, num2): 
    quot = num/num2 
    return(quot) 

main() 
+0

코드의 언어 및 형식을 설정하십시오. – m0skit0

+0

X라는 함수를 호출하고 결과를 X ('''total = total()''')라는 변수에 저장하는 것이 좋은 생각이라고 생각하십니까? 물론 작동하지 않습니다. 그리고'''''''''''''''''''''''''''''''''''''(여분의 괄호에 관한) 무엇을하고 있다고 생각합니까? – sascha

+0

아, 그래. 해당 함수의 viariable이 같은 이름을 가진 경우에는 함수를 호출 할 수 없습니다. (tot) = 합계 (num, num2)는 내가 맞으면 전체 함수에서 변수를 호출합니다. 입력을 감사하십시오. –

답변

0

당신의 기능은 당신이 뭘 하려는지 설명 당신도하지 않을 경우 그것을 써.

def total(num, num2): 
    return num+num2 

def difference(num, num2): 
    return abs(num - num2) 

def product(num, num2): 
    return num*num2 

def quotient(num, num2): 
    return num/num2 

def main(): 
    num = float(input(" Enter a number: ")) 
    num2 = float(input(" Enter another number: ")) 

    summa = total(num, num2) #try to not use the same name for a variable and a function 
    diff = difference(num, num2) 
    prod = product(num, num2) 
    quot = quotient(num, num2) 

    print(" The total is: ", format(summa, ".1f"), sep="") 
    print(" The difference is: ", format(diff, ".1f"), sep="") 
    print(" The product is: ", format(prod, ".2f"), sep="") 
    print(" The quotient is: ", format(quot, ".2f"), sep="") 

main()