【发布时间】:2021-10-02 16:58:11
【问题描述】:
我目前正在编写一个税收计算器,我注意到如果我输入了一个无效的税收代码,一开始它看起来会被拒绝,但随后程序似乎保留了初始输入并以相反的顺序循环它们(像这样):
[User Input] Gross Income: £32,000 [User Input] Tax Code: 32,000 Tax Code after input: 32,000 Failed letter check Invalid input. Please enter your tax code. [User Input] Tax Code: y Tax Code after input: y Passed letter check Tax Code after letter check: y Tax Code during dictionary match check (false): y Failed dictionary match check Invalid input. Please enter your tax code. [User Input] Tax Code: 1257L Tax Code after input: 1257L Passed letter check Tax Code after letter check: 1257L Tax Code during dictionary match check (true): 1257L Passed dictionary match check Tax Code after dictionary match check: 1257L Personal Allowance: 12570 Tax Letter: L Tax Code after dictionary match check: y Personal Allowance: 0 Tax Letter: Y Tax Code after letter check: 32,000 Traceback (most recent call last): File "C:\Users\Joshua.Riberio\Git\taxcalc\taxcalc.py", line 336, in <module> salarycalc() File "C:\Users\Joshua.Riberio\Git\taxcalc\taxcalc.py", line 243, in salarycalc personal_allowance, tax_letter = get_tax_code() File "C:\Users\Joshua.Riberio\Git\taxcalc\taxcalc.py", line 78, in get_tax_code tax_letter = tax_code[tax_letter_index:].upper() TypeError: slice indices must be integers or None or have an __index__ method
这是我的代码:
def get_tax_code(): tax_code = input('Tax Code: ') print('Tax Code after input:', tax_code) tax_letter_index = '' # Checking input contains a letter for char in tax_code: if char.upper() in alpha: tax_letter_index = tax_code.index(char) print('Passed letter check') break if tax_letter_index == '': print('Failed letter check') print('Invalid input. Please enter your tax code.') get_tax_code() print('Tax Code after letter check:', tax_code) tax_letter = tax_code[tax_letter_index:].upper() # Checking input has a key match in the tax_letters dictionary if tax_letter not in tax_letters.keys(): print('Tax Code during dictionary match check (false):', tax_code) print('Failed dictionary match check') print('Invalid input. Please enter your tax code.') get_tax_code() elif tax_letter in tax_letters.keys(): print('Tax Code during dictionary match check (true):', tax_code) print('Passed dictionary match check') print('Tax Code after dictionary match check:', tax_code) # Getting personal allowance from Tax Code personal_allowance = tax_code[:tax_letter_index] if personal_allowance == '': personal_allowance = 0 else: personal_allowance = int(personal_allowance) * 10 # Setting personal allowance exceptions for gross income over £100,000 if gross_income > 100000: personal_allowance = set_personal_allowance - ((gross_income - 100000) / 2) if personal_allowance < 0: personal_allowance = 0 print('Personal Allowance:', personal_allowance) print('Tax Letter:', tax_letter) return personal_allowance, tax_letter
过多的打印只是为了让我可以看到输入的更改位置。似乎在成功运行后,代码使用先前的无效输入循环回到顶部,导致代码无法运行。
谁能看出我哪里出错了?
备注
tax_letters 是一个已定义的字典,将对其进行更新以从选定的 CSV 中提取:
tax_letters = {
"L": "You’re entitled to the standard tax-free Personal Allowance",
"M": "Marriage Allowance: you’ve received a transfer of 10% of your partner’s Personal Allowance",
"N": "Marriage Allowance: you’ve transferred 10% of your Personal Allowance to your partner",
"T": "Your tax code includes other calculations to work out your Personal Allowance",
"0T": "Your Personal Allowance has been used up, or you’ve started a new job and your employer does not have the details they need to give you a tax code",
"BR": "All your income from this job or pension is taxed at the basic rate (usually used if you’ve got more than one job or pension)",
"D0": "All your income from this job or pension is taxed at the higher rate (usually used if you’ve got more than one job or pension)",
"D1": "All your income from this job or pension is taxed at the additional rate (usually used if you’ve got more than one job or pension)",
"NT": "You’re not paying any tax on this income",
"S": "Your income or pension is taxed using the rates in Scotland",
"S0T": "Your Personal Allowance (Scotland) has been used up, or you’ve started a new job and your employer does not have the details they need to give you a tax code",
"SBR": "All your income from this job or pension is taxed at the basic rate in Scotland (usually used if you’ve got more than one job or pension)",
"SD0": "All your income from this job or pension is taxed at the intermediate rate in Scotland (usually used if you’ve got more than one job or pension)",
"SD1": "All your income from this job or pension is taxed at the higher rate in Scotland (usually used if you’ve got more than one job or pension)",
"SD2": "All your income from this job or pension is taxed at the top rate in Scotland (usually used if you’ve got more than one job or pension)",
"C": "Your income or pension is taxed using the rates in Wales",
"C0T": "Your Personal Allowance (Wales) has been used up, or you’ve started a new job and your employer does not have the details they need to give you a tax code",
"CBR": "All your income from this job or pension is taxed at the basic rate in Wales (usually used if you’ve got more than one job or pension)",
"CD0": "All your income from this job or pension is taxed at the higher rate in Wales (usually used if you’ve got more than one job or pension)",
"CD1": "All your income from this job or pension is taxed at the additional rate in Wales (usually used if you’ve got more than one job or pension)"
}
gross_income在main函数中定义:
def salarycalc():
screen_clear()
global gross_income
gross_income = input('Gross Income: £')
if ',' in gross_income:
gross_income = gross_income.replace(',', '')
if '£' in gross_income:
gross_income = gross_income.replace('£', '')
gross_income = float(gross_income)
set_personal_allowance 是由 'tax_brackets' 字典中的值定义的变量,稍后将再次更新它以从 CSV 的选择中提取:
tax_brackets = {
'Personal Allowance': 12570,
'Basic': [0, 0.2],
'Higher': [50270, 0.4],
'Additional': [150000, 0.45]
}
set_personal_allowance = tax_brackets['Personal Allowance']
【问题讨论】:
-
tax_letters、gross_income和set_personal_allowance定义在哪里? -
@vnk 我刚刚添加了一条注释来澄清这一点,感谢您的关注
-
你的问题是在
get_tax_code()内部你再次运行get_tax_code()- 所以在完成第二个get_tax_code()之后它会回到第一个get_tax_code()并继续它。您至少应该在执行(每)第二个get_tax_code()之后使用return- 它也会首先退出get_tax_code()而不会继续它。
标签: python python-3.x validation for-loop