Python 具有多种功能的销售税程序
我的代码有问题。每次我编译程序时,都会出现一个错误,告诉我语法不正确。这是函数的正确语法吗Python 具有多种功能的销售税程序,python,Python,我的代码有问题。每次我编译程序时,都会出现一个错误,告诉我语法不正确。这是函数的正确语法吗 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 p
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
文件“”,第19行
def TotalC():
^
SyntaxError:无效语法
在代码中,函数应该有一个发送“sale”值的参数
请尝试以下方法:
def TotalC(sale):
根据您的函数,必须预先设置变量“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