如何在我的输出中消除括号和逗号?

时间:2018-11-15 07:14:45

标签: python python-3.x

我正在用python编写代码,在其中使用不同的函数来计算工资,联邦税,州税和净值。一切都很好,除了我的输出显示为('Your wage is: $', 989)而不是Your wage is: $989,而我尝试使用+(wag)却没有让我运行它。如何消除输出中的括号,逗号和引号?以及如何使输出没有小数点?我没有使用浮点数,所以我不知道为什么它仍然给我小数点。这是我的代码:

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)

3 个答案:

答案 0 :(得分:4)

对于这一部分:

print("Your wage is: $" ,wag)
print("Your federal tax is: $",FederalTax)
print("Your state tax is: $",StateTax)
print("Your net wage is: $",Net)

您可以使用字符串的format()方法将其重写为:

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)

将返回1.44

答案 1 :(得分:2)

python 3.x的输出中没有引号或括号,请检查您是在python 2还是python 3上运行。通过判断您的输出,看来您正在python 2上。

因此,请像这样更改所有打印语句

print "Your net wage is: $", wag # remove brackets
...

但是,如果您希望它在python 3上运行,则您的代码将无法运行,因为您需要在此行中乘以2个字符串

def Calculatewages():
    wage = (work*hours) # <------ here
    return wage

要解决此问题,您必须将它们强制转换为int,然后您的代码才能正常运行。

hours = int(input("Please enter your work hours: ")) # < ---- Cast to int
work = int(input("Please enter your hourly rate: ")) # < ---- Cast to int
state = input("Please enter your state of resident: ")
status = input("Please enter your marital status: ")

我的输出:

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()方法:

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))

答案 2 :(得分:0)

如果您在python3.x下运行,则使用您的代码,它应该在没有括号的情况下打印出来;如果您在python2.x下运行,则要摆脱括号,您可以尝试:

打印“您的工资是:$”,摇动