Python算法3.2 - 请帮我解决错误。

时间:2012-10-02 22:31:27

标签: python algorithm python-3.x

这是我的编码:

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美元。

我需要该物业的实际价值以及评估价值和物业税的显示。

我需要使用的功能:

  • 一个来自用户的输入
  • 一个计算所有值
  • 一个输出结果
  • 和调用其他三个函数的主要功能

2 个答案:

答案 0 :(得分:2)

你设置actualValue = None然后你尝试在函数中使用它但a)你没有分配它和b)在你在一个函数中分配它之前你必须调用全局actualValue,就像你对其他函数一样变量。如果您只是按照@cdhowie

指出的那样阅读,则不需要使用全局

由于actualValue属于Nonetype,因此您无法将其乘以另一个数字。那是你的错误。

你可以做3件事之一。

1)您有actualValue = None的地方将其更改为actualValue = 10000。

2)在main中设置actualValue,如下所示:

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

3)按照另一个答案的建议参数化你的功能。

答案 1 :(得分: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