[英]How to get rid of the parenthesis and comma in my output?
我正在用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)
對於這一部分:
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函數,如下所示:
round(float(variable), int(decimal_places))
例如:
round(1.43523556, 2)
將返回1.44
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))
如果您在python3.x下運行,則使用您的代碼,它應該在不帶括號的情況下打印出來;如果您在python2.x下運行,則為了擺脫括號,您可以嘗試:
打印“您的工資是:$”,搖擺
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.