我有此代码来计算税金。我遇到NameError: name 'raw_input' is not defined
这样的语法错误,不知道为什么?我是编码的新手,对此主题了解甚少。就像我花了很多时间写完几行,并且经过一些长期的研究之后才明白什么是我。
我写了一些东西,这给了我一个错误,我不确定自己的错误。就像我在痛苦的一段时间后做了类似的事情一样,float()
做到了这一点。不确定我缺少什么?
附注:我阅读了如何正确上传代码以供人们使用,例如最低版本。我认为我不太了解它要我做什么。很抱歉,如果它违反了规则或使其难以阅读!
# 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))
我至少想知道我做错了什么,我该如何找出解决方案?就像我想念的那样,它导致了我这个问题,而当我尝试运行时却没有?
答案 0 :(得分:0)
这是一种更清洁的编写方式,应该使用python中的某些标准约定来工作。您遇到的特定错误是因为raw_input
是python2,并且已被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}")