NameError:名称“ __”未定义

时间:2018-10-09 03:37:02

标签: python python-3.x

import sys
print ('Welcome to FouuLs\' tax calculator!')

print ('To start, enter your marital status:')

M = input('(1) Single, (2) Married filing jointly or qualifying 
widow(er), (3) Married filing separately, or (4) Head of Household: ')

#Single
if M == "Single" or M == '1':
    T = int(input('Please insert your taxable income here:'))
    R = "tax rate"
if T>0 and T<9325:
        R = .10 * T
elif T>9326 and T<37950:
        R = .15 * T + 932.50
elif T>37951 and T<91900:
        R = .25 * T + 5226.25
elif T>919101 and T<191650:
        R = .28 * T + 18713.75
elif T>191651 and T<416700:
        R = .33 * T + 46643.75
elif T>416701 and T<418400:
        R = .35 * T + 120910.25
elif 418401<T:
        R = float(.396) * float(T) + 121505.25
else:
        print ('Error: Please type a valid numerical value for taxable 
income')

tax_owed = R
print ("You owe $ {}" .format(tax_owed))
sys.exit()

#Married Filing Jointly or Qualifying Widow
if M == "Married filing jointly or qualifying widower" or M == '2':
    T = int(input('Please insert your taxable income here:'))
    R = "tax rate"
if T>0 and T<18650:
        R = .10
elif T>18651 and T<75900:
        R = .15 * 1865
elif T>75901 and T<153100:
        R = .25 * 10452.50
elif T>153101 and T<233350:
        R = .28 * 29752.50
elif T>233351 and T<416700:
        R = .33 * 52222.50
elif T>416701 and T<470700:
        R = .35 * 112728
elif 470701<T:
        R = float(.396) * float(131628)
else:
        print("Error: Please type in a valid numerical value for 
taxable income")
tax_owed = R
print ("You owe $ {}" .format(tax_owed))
sys.exit()

#Married Filing Separately
if M == "Married filing separately" or M == '3':
    T = int(input('Please insert your taxable income here:'))
    R = "tax rate"
if T>0 and T<9325:
        R = .10
elif T>9326 and T<37950:
        R = .15 * 932.50
elif T>37951 and T<76550:
        R = .25 * 5226.25
elif T>76551 and T<116675:
        R = .28 * 14876.25
elif T>116676 and T<208350:
        R = .33 * 26111.25
elif T>208351 and T<235350:
        R = .35 * 56364
elif 235351<T:
        R = .396 * 65814
else:
        print("Error: Please type in a valid numerical value for 
taxable income")
tax_owed = R
print ("You owe $ {}" .format(tax_owed))
sys.exit()

#Head of Household
if M == "Head of Household" or M == '4':
    T = int(input('Please insert your taxable income here:'))
    R = "tax rate"
if T>0 and T<9325:
        R = .10
elif T>9326 and T<37950:
        R = .15 * 1335
elif T>37951 and T<91900:
        R = .25 * 6952.50
elif T>919101 and T<191650:
        R = .28 * 27052.50
elif T>191651 and T<416700:
        R = .33 * 49816.50
elif T>416701 and T<418400:
    R = .35 * 117202.50
elif T>418401:
        R = .396 * 126950
else:
        print("Error: Please enter a valid numerical value for taxable 
income")
tax_owed = R
print ("You owe $ {}" .format(tax_owed))
sys.exit

我是编程新手,我的第一个项目遇到了这个错误:

Traceback (most recent call last):
File "/Users/mattsetaro/Desktop/taxcalc2017.py", line 12, in <module>
if T>0 and T<9325:
NameError: name 'T' is not defined

我没有得到“单”或当M = 1时出现错误,但是当M> 1时,我得到了这个错误。该脚本也完全适用于单个脚本,没有任何错误。任何帮助将不胜感激,如果您对如何改进代码有任何建议,请开除。

1 个答案:

答案 0 :(得分:0)

首先,您应该了解if ... elif ... else构造如何在python中工作,如here所示。通过正确缩进代码,可以正常工作,如下所示:

#!/usr/bin/python

import sys
print ('Welcome to FouuLs\' tax calculator!')

print ('To start, enter your marital status:')

M = input('(1) Single, (2) Married filing jointly or qualifying \
widow(er), (3) Married filing separately, or (4) Head of Household: ')

#Single
if M == "Single" or M == '1':
    T = int(input('Please insert your taxable income here:'))
    R = "tax rate"
    if T>0 and T<9325:
        R = .10 * T
    elif T>9326 and T<37950:
        R = .15 * T + 932.50
    elif T>37951 and T<91900:
        R = .25 * T + 5226.25
    elif T>919101 and T<191650:
        R = .28 * T + 18713.75
    elif T>191651 and T<416700:
        R = .33 * T + 46643.75
    elif T>416701 and T<418400:
        R = .35 * T + 120910.25
    elif 418401<T:
        R = float(.396) * float(T) + 121505.25
    else:
        print ('Error: Please type a valid numerical value for taxable \
    income')

    tax_owed = R
    print ("You owe $ {}" .format(tax_owed))
    sys.exit()

#Married Filing Jointly or Qualifying Widow
if M == "Married filing jointly or qualifying widower" or M == '2':
    T = int(input('Please insert your taxable income here:'))
    R = "tax rate"
    if T>0 and T<18650:
        R = .10
    elif T>18651 and T<75900:
        R = .15 * 1865
    elif T>75901 and T<153100:
        R = .25 * 10452.50
    elif T>153101 and T<233350:
        R = .28 * 29752.50
    elif T>233351 and T<416700:
        R = .33 * 52222.50
    elif T>416701 and T<470700:
        R = .35 * 112728
    elif 470701<T:
        R = float(.396) * float(131628)
    else:
        print("Error: Please type in a valid numerical value for \
    taxable income")
    tax_owed = R
    print ("You owe $ {}" .format(tax_owed))
    sys.exit()

#Married Filing Separately
if M == "Married filing separately" or M == '3':
    T = int(input('Please insert your taxable income here:'))
    R = "tax rate"
    if T>0 and T<9325:
        R = .10
    elif T>9326 and T<37950:
        R = .15 * 932.50
    elif T>37951 and T<76550:
        R = .25 * 5226.25
    elif T>76551 and T<116675:
        R = .28 * 14876.25
    elif T>116676 and T<208350:
        R = .33 * 26111.25
    elif T>208351 and T<235350:
        R = .35 * 56364
    elif 235351<T:
        R = .396 * 65814
    else:
        print("Error: Please type in a valid numerical value for \
    taxable income")
    tax_owed = R
    print ("You owe $ {}" .format(tax_owed))
    sys.exit()

#Head of Household
if M == "Head of Household" or M == '4':
    T = int(input('Please insert your taxable income here:'))
    R = "tax rate"
    if T>0 and T<9325:
        R = .10
    elif T>9326 and T<37950:
        R = .15 * 1335
    elif T>37951 and T<91900:
        R = .25 * 6952.50
    elif T>919101 and T<191650:
        R = .28 * 27052.50
    elif T>191651 and T<416700:
        R = .33 * 49816.50
    elif T>416701 and T<418400:
        R = .35 * 117202.50
    elif T>418401:
        R = .396 * 126950
    else:
        print("Error: Please enter a valid numerical value for taxable \
    income")
    tax_owed = R
    print ("You owe $ {}" .format(tax_owed))
    sys.exit

要注意的第二件事是,当您使用print语句或在我上面使用的input()中编写长语句时,请始终使用反斜杠。

将文件另存为fouuls_script并用python执行,输出如下:

python fouuls_script.py 
Welcome to FouuLs' tax calculator!
To start, enter your marital status:
(1) Single, (2) Married filing jointly or qualifying widow(er), (3) Married filing separately, or (4) Head of Household: 4
Please insert your taxable income here:16300
You owe $ 200.25