0

完整的初学者在这里。我一直在尝试在业余时间学习编程,但实际上并没有任何交互式资源可供参考。我已经尽力让程序在我尝试编写所得税计算器的地方工作。我已经完整地粘贴了我的程序。

我希望从中了解的是为什么该tax_calc()函数不保存变量payable。我创建了一条测试线

print ('Test Tann:', tann,'Test Tmon', tmon,'Test tinc',tinc,'test payable',payable)

为了检查 var 值,唯一不更新的是payable. 这是一个全局变量问题吗?

我也非常感谢有关我的编码的任何其他建议。这是我的第一个程序,我只知道如何在这方面使用全局变量来更改变量,尽管许多有经验的用户表示全局调用非常不必要、混乱或不符合 Python 标准。此外,非常感谢您缩短或使我的代码更高效的任何建议。

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()
4

1 回答 1

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))

这将使用值 0tinc来设置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)

您可能可以逐步重构函数以返回当前全局变量并在下一个函数中使用它,慢慢删除全局变量。

于 2013-08-15T14:15:30.443 回答