在这里完成初学者。我一直在尝试在业余时间学习编程,并且没有任何互动资源可以参考。我已经尽力让一个程序在我尝试编制所得税计算器的地方工作。我完整地粘贴了我的程序。
我希望从中理解的是tax_calc()
函数不保存变量payable
的原因。我已经创建了一个测试线
print ('Test Tann:', tann,'Test Tmon', tmon,'Test tinc',tinc,'test payable',payable)
为了检查var值,唯一没有更新的是payable
。这是一个全局变量问题吗?
我也非常感谢有关我的编码的任何其他建议。这是我的第一个程序,我真的知道如何使用全局变量来改变这方面的变量,尽管有很多有经验的用户表示全局调用是非常不必要的,混乱或单声道。此外,无论您有什么建议缩短或提高我的代码效率,我们都非常感激。
from decimal import *
#Hmm, looks like I have to define all vars and dicts before functions even if I only call functions after declaration?
tinc = 0
tann = 0
tmon = 0
age = 0
payable = 0
#Define calculation for specific tax brackets
rates = {}
rates['T1'] = 0.18 * tinc
rates['T2'] = 29808 + (.25 * (tinc - 165600))
rates['T3'] = 53096 + (.30 * (tinc - 258750))
rates['T4'] = 82904 + (.35 * (tinc - 358110))
rates['T5'] = 132894 + (.38 * (tinc - 500940))
rates['T6'] = 185205 + (.40 * (tinc - 638600))
#Defines the actual range for deciding on tax brackets
tier = {}
tier['T1'] = range(0,165600)
tier['T2'] = range(165601,258750)
tier['T3'] = range(258751,358110)
tier['T4'] = range(358111,500940)
tier['T5'] = range(500941,638600)
tier['T6'] = range(638601, 5000000)
#Defines the brackets for age variable
tierage = {}
tierage['T1'] = 12080
tierage['T2'] = 12080 + 6750
tierage['T3'] = 12080 + 6750 + 2250
#Asks for whether you want to enter monthly or annual salary
def ask_choice():
print ('Would you like to input monthly or annual salary? Please select (m/a)')
global choice
choice = str(input('> '))
#Asks for age
def ask_age():
global age
age = int(input("Please enter your age: "))
#Asks for annual salary, all inputs done in floats to allow for cents
def ask_annual():
global tann, tinc
tann = 0
tann = float(input("Please enter your annual taxable income: "))
tinc = tann
print ('Your annual taxable income is',tinc)
#Asks for monthly salary, all inputs done in floats to allow for cents
def ask_monthly():
global tmon, tinc
tmon = 0
tmon = float(input("Please enter your monthly taxable income: "))
tinc = tmon*12
print ('Your annual taxable income is',tinc)
#Decides on and calls on which function to ask for for asking salary
def asking():
global error
error = True
#keeps looping until you enter Mm or Aa
while error == True:
if choice.lower() == "m":
ask_monthly()
error == False
break
elif choice.lower() == "a":
ask_annual()
error == False
break
else:
print ("Input error, please input either 'a' to select annual or 'm' to select monthly")
error == True
ask_choice()
def tax_calc():
global payable, decpayable, tinc
if tinc in tier['T1']:
payable = rates['T1']
print ('You fall in tax bracket 1')
elif tinc in tier['T2']:
payable = rates['T2']
print ('You fall in tax bracket 2')
elif tinc in tier['T3']:
payable = rates['T3']
print ('You fall in tax bracket 3')
elif tinc in tier['T4']:
payable = rates['T4']
print ('You fall in tax bracket 4')
elif tinc in tier['T5']:
payable = rates['T5']
print ('You fall in tax bracket 5')
elif tinc in tier['T6']:
payable = rates['T6']
print ('You fall in tax bracket 6')
decpayable = Decimal(payable).quantize(Decimal('0.01'))
#Decimal used specifically for money, defines two decimal places.
print ('Tax before rebates: R',decpayable)
print ('Test Tann:', tann,'Test Tmon', tmon,'Test tinc',tinc,'test payable',payable)
def age_calc():
global final
if age < 65:
final = payable - tierage['T1']
print('You qualify for a primary rebate')
elif 65 <= age < 75:
final = payable - tierage['T2']
print('You qualify for a primary and secondary rebate')
elif age >= 75:
final = payable - tierage['T3']
print('You qualify for a primary, secondary and tertiary rebate')
decfinal = Decimal(final).quantize(Decimal('.01'))
print ('Annual tax after rebates is: R'+str(decfinal))
print ('Monthly tax is: R', Decimal(final/12).quantize(Decimal('.01')))
print ('You net salary per month is therefore: ', (tinc/12 - payable),
'or',(tinc - payable*12),'per year')
def enter_another():
print ("Would you like to calculate tax on another amount? (y/n) ")
yesno = input('> ')
if yesno.lower() == "y" or yesno.lower() == "yes":
print ('Alright, let\'s start again\n')
ask_choice()
asking()
ask_age()
tax_calc()
age_calc()
enter_another()
elif yesno.lower() == "n" or yesno.lower() == "no":
print ('Thank you for trying out this calculator')
ask_choice()
asking()
ask_age()
tax_calc()
age_calc()
enter_another()
input()
答案 0 :(得分:1)
我认为全局变量会给你带来麻烦。 你有这个靠近顶部
tinc = 0
#...
rates = {}
rates['T1'] = 0.18 * tinc
rates['T2'] = 29808 + (.25 * (tinc - 165600))
rates['T3'] = 53096 + (.30 * (tinc - 258750))
rates['T4'] = 82904 + (.35 * (tinc - 358110))
rates['T5'] = 132894 + (.38 * (tinc - 500940))
rates['T6'] = 185205 + (.40 * (tinc - 638600))
这将tinc
使用值0来设置rates
。但是,您稍后会在用户输入应税收入时使用(ask_monthly
或ask_annual
)。您需要根据tinc值来更改您使用的费率。
修改强>
如果将此更改为函数并返回字典,则可以将其传递给使用它的任何函数
def setup_rates(tinc):
rates = {}
rates['T1'] = 0.18 * tinc
rates['T2'] = 29808 + (.25 * (tinc - 165600))
rates['T3'] = 53096 + (.30 * (tinc - 258750))
rates['T4'] = 82904 + (.35 * (tinc - 358110))
rates['T5'] = 132894 + (.38 * (tinc - 500940))
rates['T6'] = 185205 + (.40 * (tinc - 638600))
return rates
更改tax_calc
以获取费率:
def tax_calc(rates):
#... as you were
然后更改“主要”功能以找到它:
asking()
ask_age()
rates = setup_rates(tinc)
tax_calc(rates)
你可以逐步重构函数来返回当前全局的变量,并在下一个函数中使用它,慢慢地删除全局变量。