它要求繳納銷售稅,但隨后列印出一長串總稅額數字
#This program will ask user for sales and calcutate state, county, and total sales tax.
#This module calculates the county tax
def askTotalSales():
totalSales=float(input("Enter sales for the month: "))
print()
return totalSales
def countyTax(totalSales):
countyTax= .02
return totalSales*countyTax
def stateTax(totalSales):
stateTax= .04
return totalSales *stateTax
#This module calculates the state tax
#this module will calculate total sales tax
def calcTotalTax(stateTax, countyTax):
totalTax=stateTax countyTax
print()
return totalTax
#printData
def printTotalTax (countyTax, stateTax, totalTax):
print ('County sales tax is' countyTax)
print('State sales tax is' stateTax)
print('Total sales tax is' totalTax)
def main():
totalSales=askTotalSales()
countySales=countyTax(totalSales)
stateSales=stateTax(totalSales)
totalTax=float(input(calcTotalTax))
main()
零指導的在線課程并不理想,我仔細閱讀了這些頁面和一些 youtube 視頻以得出這個
我知道問題可能出在我的 Cacltotal 稅務功能上——我不確定如何稱呼它
uj5u.com熱心網友回復:
問題在于這一行:
totalTax = float(input(calcTotalTax))
calcTotalTax
是函式,不是數字。該程式將列印函式地址(看起來應該是一個隨機的大數字)而不是輸出。這應該是,
totalTax = float(calcTotalTax(stateSales, countrySales))
uj5u.com熱心網友回復:
請仔細注意每個函式采用哪些引數(在def
呼叫函式時指定),并確保在呼叫函式時傳遞這些引數。另外,注意不要給變數起與函式相同的名稱;每個名字一次只能指代一件事。
在現實生活中,您通常不希望print
呼叫所有函式,但由于您在原始代碼中有它們,所以我向它們添加了字串,讓您可以看到在以下情況下呼叫了哪個函式:
def ask_total_sales():
return float(input("Enter sales for the month: "))
def calc_county_tax(total_sales):
print("Calculating county tax...")
return total_sales * 0.02
def calc_state_tax(total_sales):
print("Calculating state tax...")
return total_sales * 0.04
def calc_total_tax(county_tax, state_tax):
print("Calculating total tax...")
return county_tax state_tax
def print_total_tax(county_tax, state_tax, total_tax):
print ('County sales tax is', county_tax)
print('State sales tax is', state_tax)
print('Total sales tax is', total_tax)
def main():
total_sales = ask_total_sales()
county_tax = calc_county_tax(total_sales)
state_tax = calc_state_tax(total_sales)
total_tax = calc_total_tax(county_tax, state_tax)
print_total_tax(county_tax, state_tax, total_tax)
if __name__ == '__main__':
main()
請注意,(例如)在上面的main
函式中,我們calc_total_tax
使用引數county_tax
and呼叫該函式state_tax
,并且它回傳的內容被分配給值total_tax
,然后我們將其print_total_tax
與county_tax
and一起傳遞給state_tax
(我們通過呼叫calc_county_tax
and獲得calc_state_tax
)。
Enter sales for the month: 500
Calculating county tax...
Calculating state tax...
Calculating total tax...
County sales tax is 10.0
State sales tax is 20.0
Total sales tax is 30.0
uj5u.com熱心網友回復:
您好,您可以在此處的代碼中改進很多地方,以使您和其他任何人都能更清楚地理解。
我認為您的函式是不必要的,洗掉它們并僅在您的 main 中進行這些計算將有助于實作如此簡單的功能。
但是您的主要問題是 calcTotalTax,您不需要輸入,而且 calcTotalTax 將州稅和縣稅作為引數,但您在 main 中沒有這些。也許您想將 countySales 和 stateSales 傳遞給它?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/537587.html
標籤:Python功能