python NameError: name 'ftax' is not defined

python NameError: name 'ftax' is not defined

我已经在您的网站上阅读了几天,并尝试调试我的第一个 python 程序,该程序计算零售价 + 税金并为您提供最终价格。

这是我在 mac 上的 python IDLE 3.4.2 上运行它时遇到的错误:

Enter the retail price 0
Traceback (most recent call last):
  File "/usr/local/bin/sale tax.py", line 36, in <module>
    total = calc_total(retailprice, ptax, ftax)
NameError: name 'ftax' is not defined

按照其他帖子的建议,我尝试将 ftax 定义为 main() 之外的全局变量,然后在函数中调用它,但它不起作用。

根据没有全局变量的错误消息,下面是我的代码:

def main():
    print ('The sale tax calculator program')
    print

def input_retail():
    retailprice = input('Enter the retail price $')
    retailprice = float(retailprice)
    return retailprice

def calc_ptax(retailprice):
    ptax= retailprice * 0.099
    return ptax

def calc_ftax(retailprice):
    ftax = retailprice * 0.05
    return ftax

def calc_total(retailprice,ptax,ftax):
    total = retailprice + ptax + ftax
    return total

retailprice = input_retail()
ptax = calc_ptax(retailprice)
tax = calc_ftax(retailprice)
total = calc_total(retailprice, ptax, ftax)

def print_info(retailprice,ptax,ftax,total):

    print( 'The retailprice is $' , retailprice)
    print ('The QST amount is $' , ptax)
    print ('The GST amount is $', ftax)
    print ('The total amount is $', total)

print_info(retailprice, ptax, ftax, total)

main()

您的程序中有错字。

tax = calc_ftax(retailprice)

应该是

ftax = calc_ftax(retailprice)