我的代码有问题。每次我编译程序时,都会显示一条错误消息,告诉我语法不正确。这是函数的正确语法吗?
sale=float(input("Enter the total sales price:"))
print("Cost of item: ", format(sale, '.2f'))
def main():
c_tax()
s_tax()
TotalT()
TotalC()
def c_tax():
county_tax = sale * 0.02
print("County tax: ", format(county_tax, '.2f'))
def s_tax():
state_tax = sale * 0.04
print("State tax: ", format(state_tax, '.2f'))
def TotalT():
s = sale * 0.04
c = sale * 0.02
print("Total tax: ", format(s + c, '.2f')
def TotalC():
state = sale * 0.04
county = sale * 0.02
TotalCost = sale + county + state
print("Total cost of item: ", format(TotalCost, '.2f'))
main()
这是错误消息:
File "<ipython-input-17-176f54874857>", line 19
def TotalC():
^
SyntaxError: invalid syntax
答案 0 :(得分:0)
在您的代码中,该函数应具有一个发送“ sale”值的参数。
尝试以下方法:
def TotalC(sale):
答案 1 :(得分:0)
根据您的功能,必须预先设置变量“ sale”。
sale = 12
def TotalC():
state = sale * 0.04
county = sale * 0.02
TotalCost = sale + county + state
print("Total cost of item: ", format(TotalCost, '.2f'))
TotalC()
# 12.72
否则,您需要将其作为参数传递。
def TotalC(sale):
state = sale * 0.04
county = sale * 0.02
TotalCost = sale + county + state
print("Total cost of item: ", format(TotalCost, '.2f'))
TotalC(12)
# 12.72