我是Python的新手,我试图在下面找出如何调试我的代码。我什至不确定我是否正确创建了这四个功能。我正在尝试使用for循环显示常量和括号中的所有状态,收入和税款。你们将如何解决?
SINGLE_BASE0 = 0
SINGLE_BASE1 = 907.50
SINGLE_BASE2 = 5081.25
SINGLE_BASE3 = 18193.75
SINGLE_BASE4 = 45353.75
SINGLE_BASE5 = 117541.25
SINGLE_BASE6 = 118118.75
MARRIED_BASE0 = 0
MARRIED_BASE1 = 1815.0
MARRIED_BASE2 = 10162.5
MARRIED_BASE3 = 28925.0
MARRIED_BASE4 = 50765.0
MARRIED_BASE5 = 109587.5
MARRIED_BASE6 = 127962.5
RATE_0 = .10
RATE_1 = .15
RATE_2 = .25
RATE_3 = .28
RATE_4 = .33
RATE_5 = .35
RATE_6 = .396
SINGLE_BRACKET0 = 0
SINGLE_BRACKET1 = 9075
SINGLE_BRACKET2 = 36900
SINGLE_BRACKET3 = 89350
SINGLE_BRACKET4 = 186350
SINGLE_BRACKET5 = 405100
SINGLE_BRACKET6 = 406750
MARRIED_BRACKET0 = 0
MARRIED_BRACKET1 = 18150
MARRIED_BRACKET2 = 73800
MARRIED_BRACKET3 = 148850
MARRIED_BRACKET4 = 226850
MARRIED_BRACKET5 = 405100
MARRIED_BRACKET6 = 457600
def is_positive(income_str):
return income_str.isdigit() and int(income_str) > 0
def is_valid_status(tax_str):
return tax_str == 's' or tax_str == 'm'
def compute_tax_for_bracket(marital_status, total_income):
marital_status = ['single', 'married']
total_income = [[SINGLE_BRACKET0,SINGLE_BRACKET1, SINGLE_BRACKET1 + 1,
SINGLE_BRACKET2, SINGLE_BRACKET2 + 1,
SINGLE_BRACKET3, SINGLE_BRACKET3 + 1,
SINGLE_BRACKET4, SINGLE_BRACKET4 + 1,
SINGLE_BRACKET5, SINGLE_BRACKET5 + 1,
SINGLE_BRACKET6, SINGLE_BRACKET6 + 1],
[MARRIED_BRACKET0, MARRIED_BRACKET1, MARRIED_BRACKET1 + 1,
MARRIED_BRACKET2, MARRIED_BRACKET2 + 1,
MARRIED_BRACKET3, MARRIED_BRACKET3 + 1,
MARRIED_BRACKET4, MARRIED_BRACKET4 + 1,
MARRIED_BRACKET5, MARRIED_BRACKET5 + 1,
MARRIED_BRACKET6, MARRIED_BRACKET6 + 1]]
def compute_tax(tax_str, income_str):
status = 'single' or 'married'
income = int(income_str)
tax = ""
for i in range(len(marital_status)):
status = marital_status[i]
for j in range(len(total_income[0])):
income = total_income[i][j]
if tax_str == 's':
status = 'single'
if SINGLE_BRACKET0 <= income <= SINGLE_BRACKET1:
tax = SINGLE_BASE0 + (RATE_0 * income - SINGLE_BRACKET0 )
elif SINGLE_BRACKET1 <= income <= SINGLE_BRACKET2:
tax = (SINGLE_BASE1) + RATE_1 * (income - SINGLE_BRACKET1 )
elif SINGLE_BRACKET2 <= income <= SINGLE_BRACKET3:
tax = (SINGLE_BASE2) + RATE_2 * (income - SINGLE_BRACKET2 )
elif SINGLE_BRACKET3 <= income <= SINGLE_BRACKET4:
tax = (SINGLE_BASE3) + RATE_3 * (income - SINGLE_BRACKET3 )
elif SINGLE_BRACKET4 <= income <= SINGLE_BRACKET5:
tax = (SINGLE_BASE4) + RATE_4 * (income - SINGLE_BRACKET4 )
elif SINGLE_BRACKET5 <= income <= SINGLE_BRACKET6:
tax = (SINGLE_BASE5) + RATE_5 * (income - SINGLE_BRACKET5 )
else:
tax = (SINGLE_BASE6) + RATE_6 * (income - SINGLE_BRACKET6 )
if tax_str == 'm':
status = 'married'
if MARRIED_BRACKET0 <= income <= MARRIED_BRACKET1:
tax = MARRIED_BASE0 + (RATE_0 * income - MARRIED_BRACKET0 )
elif MARRIED_BRACKET1 <= income <= MARRIED_BRACKET2:
tax = (MARRIED_BASE1) + RATE_1 * (income - MARRIED_BRACKET1 )
elif MARRIED_BRACKET2 <= income <= MARRIED_BRACKET3:
tax = (MARRIED_BASE2) + RATE_2 * (income - MARRIED_BRACKET2 )
elif MARRIED_BRACKET3 <= income <= MARRIED_BRACKET4:
tax = (MARRIED_BASE3) + RATE_3 * (income - MARRIED_BRACKET3 )
elif MARRIED_BRACKET4 <= income <= MARRIED_BRACKET5:
tax = (MARRIED_BASE4) + RATE_4 * (income - MARRIED_BRACKET4 )
elif MARRIED_BRACKET5 <= income <= MARRIED_BRACKET6:
tax = (MARRIED_BASE5) + RATE_5 * (income - MARRIED_BRACKET5 )
else:
tax = (MARRIED_BASE6) + RATE_6 * (income - MARRIED_BRACKET6 )
print("%s, $%9.2.f = $%9.2f" % (status, income_str, tax))
print("Your tax bill, given that you are a {} filer with an income of \
{} is ${}" .format(status, income_str, tax))
tax_str = input("Enter your marital status (Enter 's' or 'm' only) OR \
press <ENTER> to quit: ")
print()
Calculate_Taxes(tax_str)
def Calculate_Taxes(tax_str):
while not is_valid_status(tax_str):
print()
tax_str = input("Enter your marital status Enter 's' or 'm' only: ")
income_str = input("Enter your income: ")
while not is_positive(income_str):
print()
income_str = input("Income must be a valid amount >= $0.00 \n"
"Enter your income: ")
compute_tax(tax_str, income_str)
def main():
print("This program computes the simple tax for single and married fillers.")
print()
tax_str = input("Enter your marital status (Enter 's' or 'm' only) OR \
press <ENTER> to quit: ")
Calculate_Taxes(tax_str)
main()
由于这段代码,很明显我得到了NameError。我相信如果将功能组合在一起可能会起作用,但是我必须将它们分开。
答案 0 :(得分:1)
从上面的评论继续,我强烈建议一些事情。
使用list
而不是list of variables
您只能使用几个循环。
有些您甚至没有使用的变量,请删除它们。
修复:
SINGLE_BASE0 = 0
SINGLE_BASE1 = 907.50
SINGLE_BASE2 = 5081.25
SINGLE_BASE3 = 18193.75
SINGLE_BASE4 = 45353.75
SINGLE_BASE5 = 117541.25
SINGLE_BASE6 = 118118.75
MARRIED_BASE0 = 0
MARRIED_BASE1 = 1815.0
MARRIED_BASE2 = 10162.5
MARRIED_BASE3 = 28925.0
MARRIED_BASE4 = 50765.0
MARRIED_BASE5 = 109587.5
MARRIED_BASE6 = 127962.5
RATE_0 = .10
RATE_1 = .15
RATE_2 = .25
RATE_3 = .28
RATE_4 = .33
RATE_5 = .35
RATE_6 = .396
SINGLE_BRACKET0 = 0
SINGLE_BRACKET1 = 9075
SINGLE_BRACKET2 = 36900
SINGLE_BRACKET3 = 89350
SINGLE_BRACKET4 = 186350
SINGLE_BRACKET5 = 405100
SINGLE_BRACKET6 = 406750
MARRIED_BRACKET0 = 0
MARRIED_BRACKET1 = 18150
MARRIED_BRACKET2 = 73800
MARRIED_BRACKET3 = 148850
MARRIED_BRACKET4 = 226850
MARRIED_BRACKET5 = 405100
MARRIED_BRACKET6 = 457600
def is_positive(income_str):
return income_str[1:].isdigit() and int(income_str[1:]) > 0
def is_valid_status(tax_str):
return tax_str == 's' or 'm'
def compute_tax_for_bracket(marital_status, total_income):
marital_status = ['single', 'married']
total_income = [[SINGLE_BRACKET0,SINGLE_BRACKET1, SINGLE_BRACKET1 + 1,
SINGLE_BRACKET2, SINGLE_BRACKET2 + 1,
SINGLE_BRACKET3, SINGLE_BRACKET3 + 1,
SINGLE_BRACKET4, SINGLE_BRACKET4 + 1,
SINGLE_BRACKET5, SINGLE_BRACKET5 + 1,
SINGLE_BRACKET6, SINGLE_BRACKET6 + 1],
[MARRIED_BRACKET0, MARRIED_BRACKET1, MARRIED_BRACKET1 + 1,
MARRIED_BRACKET2, MARRIED_BRACKET2 + 1,
MARRIED_BRACKET3, MARRIED_BRACKET3 + 1,
MARRIED_BRACKET4, MARRIED_BRACKET4 + 1,
MARRIED_BRACKET5, MARRIED_BRACKET5 + 1,
MARRIED_BRACKET6, MARRIED_BRACKET6 + 1]]
def compute_tax(tax_str, income_str):
status = 'single' or 'married'
income = income_str[1:]
tax = ""
if tax_str == 's':
status = 'single'
if SINGLE_BRACKET0 <= int(income) <= SINGLE_BRACKET1:
tax = SINGLE_BASE0 + (RATE_0 * int(income) - SINGLE_BRACKET0 )
elif SINGLE_BRACKET1 <= int(income) <= SINGLE_BRACKET2:
tax = (SINGLE_BASE1) + RATE_1 * (int(income) - SINGLE_BRACKET1 )
elif SINGLE_BRACKET2 <= int(income) <= SINGLE_BRACKET3:
tax = (SINGLE_BASE2) + RATE_2 * (int(income) - SINGLE_BRACKET2 )
elif SINGLE_BRACKET3 <= int(income) <= SINGLE_BRACKET4:
tax = (SINGLE_BASE3) + RATE_3 * (int(income) - SINGLE_BRACKET3 )
elif SINGLE_BRACKET4 <= int(income) <= SINGLE_BRACKET5:
tax = (SINGLE_BASE4) + RATE_4 * (int(income) - SINGLE_BRACKET4 )
elif SINGLE_BRACKET5 <= int(income) <= SINGLE_BRACKET6:
tax = (SINGLE_BASE5) + RATE_5 * (int(income) - SINGLE_BRACKET5 )
else:
tax = (SINGLE_BASE6) + RATE_6 * (int(income) - SINGLE_BRACKET6 )
if tax_str == 'm':
status = 'married'
if MARRIED_BRACKET0 <= int(income) <= MARRIED_BRACKET1:
tax = MARRIED_BASE0 + (RATE_0 * int(income) - MARRIED_BRACKET0 )
elif MARRIED_BRACKET1 <= int(income) <= MARRIED_BRACKET2:
tax = (MARRIED_BASE1) + RATE_1 * (int(income) - MARRIED_BRACKET1 )
elif MARRIED_BRACKET2 <= int(income) <= MARRIED_BRACKET3:
tax = (MARRIED_BASE2) + RATE_2 * (int(income) - MARRIED_BRACKET2 )
elif MARRIED_BRACKET3 <= int(income) <= MARRIED_BRACKET4:
tax = (MARRIED_BASE3) + RATE_3 * (int(income) - MARRIED_BRACKET3 )
elif MARRIED_BRACKET4 <= int(income) <= MARRIED_BRACKET5:
tax = (MARRIED_BASE4) + RATE_4 * (int(income) - MARRIED_BRACKET4 )
elif MARRIED_BRACKET5 <= int(income) <= MARRIED_BRACKET6:
tax = (MARRIED_BASE5) + RATE_5 * (int(income) - MARRIED_BRACKET5 )
else:
tax = (MARRIED_BASE6) + RATE_6 * (int(income) - MARRIED_BRACKET6 )
print("Your tax bill, given that you are a {} filer with an income of \
{} is ${}".format(status, income_str, tax))
def main():
print("This program computes the simple tax for single and married fillers.")
print()
tax_str = input("Enter your marital status (Enter 's' or 'm' only) OR \
press <ENTER> to quit: ")
while not is_valid_status(tax_str):
print()
tax_str = input("Enter your marital status Enter 's' or 'm' only: ")
income_str = input("Enter your income: ")
while not is_positive(income_str):
print()
income_str = input("Income must be a valid amount >= $0.00 \n"
"Enter your income: ")
compute_tax(tax_str, income_str)
tax_str = input("Enter your marital status (Enter 's' or 'm' only) OR \
press <ENTER> to quit: ")
main()
输出:
This program computes the simple tax for single and married fillers.
Enter your marital status (Enter 's' or 'm' only) OR press <ENTER> to quit: s
Enter your income: $9075
Your tax bill, given that you are a single filer with an income of $9075 is $907.5
Enter your marital status (Enter 's' or 'm' only) OR press <ENTER> to quit:
编辑:
添加了一种方法Calculate_Taxes(tax_str)
来继续计算税收。
def is_positive(income_str):
return income_str[1:].isdigit() and float(income_str[1:]) > 0
def is_valid_status(tax_str):
return tax_str == 's' or 'm'
def compute_tax_for_bracket(marital_status, total_income):
marital_status = ['single', 'married']
total_income = [[SINGLE_BRACKET0,SINGLE_BRACKET1, SINGLE_BRACKET1 + 1,
SINGLE_BRACKET2, SINGLE_BRACKET2 + 1,
SINGLE_BRACKET3, SINGLE_BRACKET3 + 1,
SINGLE_BRACKET4, SINGLE_BRACKET4 + 1,
SINGLE_BRACKET5, SINGLE_BRACKET5 + 1,
SINGLE_BRACKET6, SINGLE_BRACKET6 + 1],
[MARRIED_BRACKET0, MARRIED_BRACKET1, MARRIED_BRACKET1 + 1,
MARRIED_BRACKET2, MARRIED_BRACKET2 + 1,
MARRIED_BRACKET3, MARRIED_BRACKET3 + 1,
MARRIED_BRACKET4, MARRIED_BRACKET4 + 1,
MARRIED_BRACKET5, MARRIED_BRACKET5 + 1,
MARRIED_BRACKET6, MARRIED_BRACKET6 + 1]]
def compute_tax(tax_str, income_str):
status = 'single' or 'married'
income = income_str[1:]
tax = ""
if tax_str == 's':
status = 'single'
if SINGLE_BRACKET0 <= int(income) <= SINGLE_BRACKET1:
tax = SINGLE_BASE0 + (RATE_0 * int(income) - SINGLE_BRACKET0 )
elif SINGLE_BRACKET1 <= int(income) <= SINGLE_BRACKET2:
tax = (SINGLE_BASE1) + RATE_1 * (int(income) - SINGLE_BRACKET1 )
elif SINGLE_BRACKET2 <= int(income) <= SINGLE_BRACKET3:
tax = (SINGLE_BASE2) + RATE_2 * (int(income) - SINGLE_BRACKET2 )
elif SINGLE_BRACKET3 <= int(income) <= SINGLE_BRACKET4:
tax = (SINGLE_BASE3) + RATE_3 * (int(income) - SINGLE_BRACKET3 )
elif SINGLE_BRACKET4 <= int(income) <= SINGLE_BRACKET5:
tax = (SINGLE_BASE4) + RATE_4 * (int(income) - SINGLE_BRACKET4 )
elif SINGLE_BRACKET5 <= int(income) <= SINGLE_BRACKET6:
tax = (SINGLE_BASE5) + RATE_5 * (int(income) - SINGLE_BRACKET5 )
else:
tax = (SINGLE_BASE6) + RATE_6 * (int(income) - SINGLE_BRACKET6 )
if tax_str == 'm':
status = 'married'
if MARRIED_BRACKET0 <= int(income) <= MARRIED_BRACKET1:
tax = MARRIED_BASE0 + (RATE_0 * int(income) - MARRIED_BRACKET0 )
elif MARRIED_BRACKET1 <= int(income) <= MARRIED_BRACKET2:
tax = (MARRIED_BASE1) + RATE_1 * (int(income) - MARRIED_BRACKET1 )
elif MARRIED_BRACKET2 <= int(income) <= MARRIED_BRACKET3:
tax = (MARRIED_BASE2) + RATE_2 * (int(income) - MARRIED_BRACKET2 )
elif MARRIED_BRACKET3 <= int(income) <= MARRIED_BRACKET4:
tax = (MARRIED_BASE3) + RATE_3 * (int(income) - MARRIED_BRACKET3 )
elif MARRIED_BRACKET4 <= int(income) <= MARRIED_BRACKET5:
tax = (MARRIED_BASE4) + RATE_4 * (int(income) - MARRIED_BRACKET4 )
elif MARRIED_BRACKET5 <= int(income) <= MARRIED_BRACKET6:
tax = (MARRIED_BASE5) + RATE_5 * (int(income) - MARRIED_BRACKET5 )
else:
tax = (MARRIED_BASE6) + RATE_6 * (int(income) - MARRIED_BRACKET6 )
print("Your tax bill, given that you are a {} filer with an income of \
{} is ${}".format(status, income_str, tax))
tax_str = input("Enter your marital status Enter 's' or 'm' only or q/Q to exit: ")
if tax_str == 'Q'.lower():
exit()
else:
Calculate_Taxes(tax_str)
def Calculate_Taxes(tax_str):
while not is_valid_status(tax_str):
print()
tax_str = input("Enter your marital status Enter 's' or 'm' only: ")
income_str = input("Enter your income: ")
while not is_positive(income_str):
print()
income_str = input("Income must be a valid amount >= $0.00 \n"
"Enter your income: ")
compute_tax(tax_str, income_str)
def main():
print("This program computes the simple tax for single and married fillers.")
print()
tax_str = input("Enter your marital status (Enter 's' or 'm' only) OR \
press <ENTER> to quit: ")
Calculate_Taxes(tax_str)
main()
答案 1 :(得分:0)
您陷入了第一个while循环while is_valid_status(tax_str):
。我认为您甚至不需要它,您应该分开保留2个。
while not is_valid_status(tax_str):
print()
tax_str = input("Enter your marital status Enter 's' or 'm' only: ")
income_str = input("Enter your income: ")
while not is_positive(income_str):
print()
income_str = input("Income must be a valid amount >= $0.00 \n"
"Enter your income: ")
compute_tax(tax_str, income_str)