0

I have this code to calculate tax. I am having an syntax error like this NameError: name 'raw_input' is not defined and don't know why? I am quite new to coding and have little understanding about the subject. Like it takes me forever to write few lines and understand what is what after some long research.

I wrote something and it gives me an error, and I am not really sure about my error. Like I did something similar after an agonizing time later, and float() did the trick with it. Not sure what I am missing? P.S: I read the how to upload the code properly for people to approach it, like minimal version. I don't think I quite grasped what it wants me to do. Sorry if that is violated a rule or made it harder to read!

# input of tax status
tax_status = raw_input('Enter your tax status(single or married) : ')
# validate tax status
while tax_status.strip().lower() != 'single' and tax_status.strip().lower() != 'married':
    print('Invalid value. Tax status can be eiher single or married')
    tax_status = raw_input('Enter your tax status(single or married) : ')
#input of income             
income = float(raw_input('Enter your income: '))
# validate income > 0
while income <= 0 :
    print('Invalid value. Income must be greater than 0')
    income = float(raw_input('Enter your income: '))
tax_amount = 0
# calculate tax amount based on tax_status and income
if tax_status == 'single':
    if income <= 9700:
        tax_amount = (10*income)/100
    elif income <= 39475:
        tax_amount = (12*income)/100
    elif income <= 84200:
        tax_amount = (22*income)/100
    elif income <=160725:
        tax_amount = (24*income)/100
    elif income <= 204100:
        tax_amount = (32*income)/100
    elif income <= 510300:
        tax_amount = (35*income)/100
    else:
        tax_amount = (37*income)/100
else:
    if income <= 19400:
        tax_amount = (10*income)/100
    elif income <= 78950:
        tax_amount = (12*income)/100
    elif income <= 168400:
        tax_amount = (22*income)/100
    elif income <=321450:
        tax_amount = (24*income)/100
    elif income <= 408200:
        tax_amount = (32*income)/100
    elif income <= 612350:
        tax_amount = (35*income)/100
    else:
        tax_amount = (37*income)/100
# output the tax amount               
print('Your tax amount is $%.2f' %(tax_amount))                

I at least want to know what I did wrong, and how can I figure out to make it run? Like what I missed that it causes me this issue, that when I try to run it doesn't?

martineau
  • 119,623
  • 25
  • 170
  • 301
Ibrahim
  • 9
  • 1
  • 2
    please check this answer : https://stackoverflow.com/a/954840/11542188 – Masoud Jul 09 '19 at 20:40
  • I would recommend getting rid of using all the if statements and using a map or a dictionary. (https://stackoverflow.com/questions/60208/replacements-for-switch-statement-in-python) – Duncan McFarlane Jul 09 '19 at 20:41
  • Can you be more specific which line is giving you the error? (p.s. in the future you can just paste in your code, then highlight it all and press the button that looks like a set of curly-brackets) – Green Cloak Guy Jul 09 '19 at 20:44
  • @DuncanMcFarlane Maps aren't as useful for a series of range checks. – chepner Jul 09 '19 at 21:00
  • @chepner You could use an enumerator as I did in my answer. But you are correct, I jumped to a conclusion when I saw all the branches. – Duncan McFarlane Jul 09 '19 at 21:02
  • @GreenCloakGuy Line 3 is the one gave the error in the first place. – Ibrahim Jul 09 '19 at 21:17
  • @DuncanMcFarlane Hey! Thank you for an such reply! May I use your code for my personal use? Also how can I credit you for clearing the code? – Ibrahim Jul 09 '19 at 21:18
  • @Ibrahim No credit is needed and it should work properly. If it doesn't let me know as I might have a made a typo somewhere. If you really want to credit me I guess just link my stack overflow profile. – Duncan McFarlane Jul 09 '19 at 21:19
  • Thank you @DuncanMcFarlane if I need to use this besides the personal use I will do so! I have encountered an issue in "Line 7" "TypeError: 'list' object cannot be interpreted as an integer" – Ibrahim Jul 09 '19 at 21:26
  • Thank you for the support so far people who have answered and edited my post to make it more understandable!! – Ibrahim Jul 09 '19 at 21:28
  • I fixed the code and it should work now :) – Duncan McFarlane Jul 09 '19 at 21:31
  • @DuncanMcFarlane It works for the the single income though for some reason doesn't for the married one. I tried to capitalize the letters on married to resemble the single one but "Line 13" "line 13, in for multiplier in MarriedIncomeLevel[multiplier]: NameError: name 'multiplier' is not defined" keeps giving error when I file as married. – Ibrahim Jul 09 '19 at 21:48
  • Sorry I rushed, works fine now. – Duncan McFarlane Jul 10 '19 at 18:03

1 Answers1

0

Here is a cleaner way of writing that should work using some of the standard conventions in python. The specific error you were encountering is because raw_input is python2 and was replaced with input.

income = int(input('Enter your income: '))
tax_status = input('Enter your tax status(single or married) : ').lower()

if tax_status == 'single':
    SingleIncomeLevel = {10:9700, 12:39475, 22:84200, 24:160725, 32:204100, 35:510300}
    for multiplier in SingleIncomeLevel:
        if income <= SingleIncomeLevel[multiplier]:
            tax_amount = (multiplier*income)/100
        else:
            tax_amount = (37*income)/100
else:
    marriedIncomeLevel = {10:19400, 12:78950, 22:168400, 24:321450, 32:408200, 35:612350}
    for multiplier in marriedIncomeLevel:
        if income <= marriedIncomeLevel[multiplier]:
            tax_amount = (multiplier*income)/100
        else:
            tax_amount = (37*income)/100
print(f"Your tax amount is {tax_amount}")