如何修复此语法错误?我很迷惑

时间:2012-09-22 06:08:31

标签: python syntax-error

以下是我所做的所有编码,但我一直收到此语法错误。它将在底部详细解释。

def main():
    ActualValue()
    AssessedValue()
    printResult()

def ActualValue()
    global actual_value
    actual_value = float(input("Enter actual value:\t"))

def AssessedValue()
    global assessed_value
    global property_tax
    assessed_value = 0.6 * actual_value
    property_tax = assessed_value / 100 * 0.64

def printResult():
    print "n\For a property valued at $", actual_value
    print "The assessed value is $", assessed_value
    print "The property tax is $", property_tax

actual_value = None
assessed_value = None
property_tax = None

main()

那是我的代码:

它一直说我有语法错误:

def printResult():
    print "n\For a property valued at $", actual_value
    print "The assessed value is $", assessed_value
    print "The property tax is $", property_tax

3 个答案:

答案 0 :(得分:5)

你有向后的\n转义序列。

此外,您需要确保所有函数定义在行的末尾都有冒号。

此外,print是Python 3中的一个函数。

答案 1 :(得分:1)

print是Python 3中的一个函数:

def printResult():
    print("\nFor a property valued at $", actual_value)
    print("The assessed value is $", assessed_value)
    print("The property tax is $", property_tax)

我也为您修复了\n换行符代码。

您可能希望使用.format() method格式化输出:

def printResult():
    print("\nFor a property valued at ${0}".format(actual_value))
    print("The assessed value is ${0}".format(assessed_value))
    print("The property tax is ${0}".format(property_tax))

答案 2 :(得分:0)

只是为了澄清Platinum Azure所说的内容。

def main():
    actualValue()
    assessedValue()
    printResult()

def actualValue():
    global actual_value
    actual_value = float(input("Enter actual value:\t"))

def assessedValue():
    global assessed_value
    global property_tax
    assessed_value = 0.6 * actual_value
    property_tax = assessed_value / 100 * 0.64

def printResult():
    print "\nFor a property valued at $", actual_value
    print "The assessed value is $", assessed_value
    print "The property tax is $", property_tax

actual_value = None
assessed_value = None
property_tax = None

main()

这应该有效