这里完全是初学者。我一直在尝试在业余时间学习编程,实际上没有任何交互资源可供参考。我已经尽我最大的努力使一个程序工作,在那里,我试图设计一个所得税计算器。我把我的整个程序都贴上了。
我希望从这里了解为什么tax_calc()函数没有保存变量payable。我已经创建了一个测试行
print ('Test Tann:', tann,'Test Tmon', tmon,'Test tinc',tinc,'test payable',payable)为了检查var值,唯一不更新的值是payable。这是一个全局变量问题吗?
我也非常感谢关于我的编码的任何其他建议。这是我的第一个程序,我只知道如何使用globals在这方面改变vars,尽管许多有经验的用户表示,全球调用是非常不必要的,混乱或非unpythonic。此外,无论您有什么建议来缩短或使我的代码更有效率,我们都非常感激。
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()发布于 2013-08-15 14:15:30
我认为全局变量给你带来了麻烦。你把这个放在最上面
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然后更改"main“函数以找到它:
asking()
ask_age()
rates = setup_rates(tinc)
tax_calc(rates)您可能可以逐步重构函数以返回当前全局的变量,并在下一个函数中使用该变量,缓慢地删除全局变量。
https://stackoverflow.com/questions/18254180
复制相似问题