2017-02-21 13 views
0

如何將這些變量返回到主函數? (這是Class的一部分,我不能使用Global,另外我必須打印除main函數以外的錯誤。獲取錯誤,如AssessedTotalValue未分配,PropertyTaxTotal未分配。它用於將Parameter變量獲取到函數中,但爲什麼arn't他們現身其中?我試圖在Python中切換參數而不使用全局變量

def PropertyTax(): 
    global PropertyTaxPercent 
    PropertyTaxPercent = .64 
    PropertyValue = int(input("Enter Property Value: ")) 
    AssessedValue(PropertyValue) 
    PropertyTaxValue(AssessedTotalValue) 
    print ("The Property Assessed Value is: ", AssessedTotalValue) 
    print ("The Property Tax is: ", PropertyTaxTotal) 

def AssessedValue(PropertyValue): 
    global AssessedPercentValue 
    AssessedPercentValue = 0.60 
    AssessedTotalValue = PropertyValue * AssessedPercentValue 

def PropertyTaxValue(AssessedTotalValue): 
    PropertyTaxValue = AssessedTotalValue/100 
    PropertyTaxTotal = PropertyTaxValue * PropertyTaxPercent 
PropertyTax() 
+0

你」我已經瞭解了爭論。現在查找「返回值」。 – user2357112

回答

0

你並不需要定義的變量爲全局。 可以使用return語句在你的功能,這將使你的職責還給值,您呼叫的地方功能from。

例如

def add(a,b): 
    total = a + b 
    return total 

總和=添加(1,6) 打印(和)

將輸出7.


鑑於這種你可以修改你的程序如下:

def PropertyTax(): 

    PropertyTaxPercent = .64 
    PropertyValue = int(input("Enter Property Value: ")) 
    AssessedTotalValue = AssessedValue(PropertyValue) 
    PropertyTaxTotal = PropertyTaxValue(AssessedTotalValue,PropertyTaxPercent) 
    print ("The Property Assessed Value is: ", AssessedTotalValue) 
    print ("The Property Tax is: ", PropertyTaxTotal) 

def AssessedValue(PropertyValue): 
    AssessedPercentValue = 0.60 
    AssessedTotalValue = PropertyValue * AssessedPercentValue 
    return AssessedTotalValue 

def PropertyTaxValue(AssessedTotalValue,PropertyTaxPercent): 
    PropertyTaxValue = AssessedTotalValue/100 
    PropertyTaxTotal = PropertyTaxValue * PropertyTaxPercent 
    return PropertyTaxTotal 

PropertyTax()