2012-10-02 43 views
0

這裏是我的編碼:Python算法3.2 - 請幫我解決錯誤。

def main(): 
    actualValued() 
    assessed_value() 
    printResult() 

def actualValued(): 
    global assessed_value 
    global property_tax 
    assessed_value = 0.6 * actualValue 
    property_tax = assessed_value/100*0.64 

def printResult(): 
    print("For a property valued at"), actualValued 
    print("The assessed value is"), asessed_value 
    print("The property tax is"), property_tax 

actualValue = None 
assessed_value = None 
property_tax = None 

main() 

的錯誤:

Traceback (most recent call last): 
File "C:/Documents and Settings/Desktop/property tax.py", line 21, in <module> 
main() 

File "C:/Documents and Settings/Desktop/property tax.py", line 2, in main 
actualValued() 

File "C:/Documents and Settings/Desktop/property tax.py", line 9, in actualValued 
assessed_value = 0.6 * actualValue 

TypeError: unsupported operand type(s) for *: 'float' and 'NoneType' 
>>> 

我所試圖做的事:

回車評估值爲10000.0 對於價值$ 10,000.00 的評估值的屬性是$ 6,000.00 而稅是$ 38.40

物業稅:一個縣徵收物業評估價值的財產稅,這是物業實際價值的60%。例如,如果一英畝土地的價值爲10,000美元,則其評估價值爲6,000美元。對於每100美元的評估價值,物業稅是64%。評估價爲6,000美元的英畝稅將爲38.40美元。

我需要該物業的實際價值和顯示評估價值和物業稅。

功能,我需要使用:

  • 一個從用戶
  • 一個獲得輸入計算所有值
  • 一個輸出結果
  • 和主函數來調用三個其他功能
+0

它的蟒蛇3.0 –

回答

2

您設置actualValue = None,然後嘗試在函數中使用它,但a)您永遠不會分配它,並b)在將其分配給函數之前,必須像調用其他變量一樣調用全局actualValue。如果您只是按照@cdhowie指出的方式閱讀,則不需要全局使用

由於actualValue是Nonetype,因此您無法將其乘以另一個數字。那是你的錯誤。

你可以做3件事之一。

1)其中您有actualValue = None將其更改爲actualValue = 10000。

2)設置actualValue在主要如下:

def main(): 
    global actualValue 
    actualValue = 10000 
    ... 

3)參數您的函數由另一個答案的建議。

+0

沒關係,他不使用'global' - 他只從變量讀取,並且不寫它。 'global'只在嘗試將*寫入到父範圍內的變量時非常重要。 – cdhowie

+0

是真實的,除了他需要在某處寫信。我將編輯帖子。 – ajon

+0

是的,他確實 - 這就是我對你的答案滿意的原因。但關於'global'的評論是不正確的,只是一個紅鯡魚。 :) – cdhowie

1

您可能想要重寫代碼,以便它使用參數並返回值而不是全局變量。

def actualValued(actualValue): 
    assessed_value = 0.6 * actualValue 
    property_tax = assessed_value/100*0.64 
    return assessed_value, property_tax 

# get your actual value from user input e.g. 
value = raw_input('Give actual value: ') 
value = float(value) 
assessed, tax = actualValued(value) 

print("For a property valued at"), valued 
print("The assessed value is"), asessed 
print("The property tax is"), tax