Python 如何去掉输出中的括号和逗号?
我正在用python编写一段代码,其中我使用不同的函数来计算工资、联邦税、州税和净值。一切正常,但我的输出显示,Python 如何去掉输出中的括号和逗号?,python,python-3.x,Python,Python 3.x,我正在用python编写一段代码,其中我使用不同的函数来计算工资、联邦税、州税和净值。一切正常,但我的输出显示,(“您的工资是:$”,989)而不是您的工资是:$989我尝试使用+(wag),但它不允许我运行它。如何从输出中去掉括号、逗号和引号?如何使输出没有小数点?我没有使用float,所以我不知道为什么它仍然给我小数点。这是我的密码: def Calculatewages(): wage = (work*hours) return wage def CalcualteFede
(“您的工资是:$”,989)
而不是您的工资是:$989
我尝试使用+(wag)
,但它不允许我运行它。如何从输出中去掉括号、逗号和引号?如何使输出没有小数点?我没有使用float,所以我不知道为什么它仍然给我小数点。这是我的密码:
def Calculatewages():
wage = (work*hours)
return wage
def CalcualteFederaltax():
if (status== ("Married")):
federaltax = 0.20
elif (status== ("Single")):
federaltax = 0.25
elif status:
federaltax = 0.22
federaltax = float(wag*federaltax)
return federaltax
def Calculatestatetax():
if(state=="CA") or (state=="NV") or (state=="SD") or (state=="WA") or (state=="AZ"):
statetax = 0.08
if (state== "TX") or(state=="IL") or (state=="MO") or (state=="OH") or (state=="VA"):
statetax = 0.07
if (state== "NM") or (state== "OR") or (state=="IN"):
statetax = 0.06
if (state):
statetax = 0.05
statetax = float(wag*statetax)
return statetax
def Calculatenet():
net = float(wag-FederalTax-StateTax)
return net
hours = input("Please enter your work hours: ")
work = input("Please enter your hourly rate: ")
state = input("Please enter your state of resident: ")
status = input("Please enter your marital status: ")
print("**********")
wag = Calculatewages()
FederalTax = CalcualteFederaltax()
StateTax = Calculatestatetax()
Net = Calculatenet()
print("Your wage is: $" ,wag)
print("Your federal tax is: $",FederalTax)
print("Your state tax is: $",StateTax)
print("Your net wage is: $",Net)
python 3.x中的输出中没有引号或括号,请检查您是否正在运行python 2
或python 3
。通过判断您的输出,看起来您正在使用python2
所以,像这样更改所有打印语句
print "Your net wage is: $", wag # remove brackets
...
但是,如果您希望它在Python3上运行,那么您的代码不会运行,因为您在此行中要乘以2个字符串
def Calculatewages():
wage = (work*hours) # <------ here
return wage
我的输出:
Please enter your work hours: 8
Please enter your hourly rate: 10
Please enter your state of resident: IN
Please enter your marital status: Single
**********
Your wage is: $ 80
Your federal tax is: $ 20.0
Your state tax is: $ 4.0
Your net wage is: $ 56.0
也可以使用字符串的format()方法:
如果您在python3.x下运行,那么使用您的代码,它应该打印出来而不带括号;如果您在python2.x下运行,那么要去掉括号,您可能需要尝试:
打印“您的工资为:$”,wag用于此部分:
print("Your wage is: $" ,wag)
print("Your federal tax is: $",FederalTax)
print("Your state tax is: $",StateTax)
print("Your net wage is: $",Net)
可以使用字符串的format()方法将其重写为:
这很有用,因为您可以将值插入字符串中的任何位置(无论放在何处)
对于小数点问题,您可以使用内置的round函数,如下所示:
round(float(variable), int(decimal_places))
例如:
round(1.43523556, 2)
将返回1.44它是否成功运行?你怎么知道输出中有偏执和逗号而没有实际看到输出?你认为你在Python 3上,但输出说你不是。@Austin我在c9.io上运行了它,这是我老师提供的一个网站,它工作正常,但由于一些奇怪的原因,只有括号、逗号和引号哦,好吧,我现在明白了,但是您知道如何去掉$
和输出编号之间的空格吗?@H.Khan从print语句中删除$
print“你的净工资是:”,wag
像这样抱歉,我的意思是让它输出$20
而不是$20.0
,下面是如何做到这一点,print“你的净工资是:${}”。格式(wag)
print("Your wage is: ${}".format(wag))
print("Your federal tax is: ${}".format(FederalTax))
print("Your state tax is: ${}".format(StateTax))
print("Your net wage is: ${}".format(Net))
round(float(variable), int(decimal_places))
round(1.43523556, 2)