Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果雇员未满30岁,他只需支付其应纳税所得额的一半,那么如何可能在该法典中增加条件?_Python_Calculation_Tax - Fatal编程技术网

Python 如果雇员未满30岁,他只需支付其应纳税所得额的一半,那么如何可能在该法典中增加条件?

Python 如果雇员未满30岁,他只需支付其应纳税所得额的一半,那么如何可能在该法典中增加条件?,python,calculation,tax,Python,Calculation,Tax,然后,我必须为计算设置一个条件,如果“年龄”,您可以使用income\u input.items()访问income\u input中的dict,该dict包含收入和年龄(见下文infodict)。然后,您可以通过它们的密钥访问它们 income_input = {'Gerard': {'age': 50, 'salary': 120000}, 'Tom': {'age': 28,'salary': 60000},

然后,我必须为计算设置一个条件,如果“年龄”,您可以使用
income\u input.items()
访问
income\u input
中的dict,该dict包含收入和年龄(见下文
info
dict)。然后,您可以通过它们的密钥访问它们

income_input  = {'Gerard': 
            {'age': 50, 'salary': 120000},
            'Tom':
         {'age': 28,'salary': 60000},
          'Roos':
         { 'age': 25,'salary': 40000}
} 
income\u输入={
杰拉德:{“年龄”:50,“薪水”:120000},
“汤姆”:{“年龄”:28,“工资”:60000},
“Roos”:{“年龄”:25,“工资”:40000}
}
def计算税(收入输入):
税条={}
对于名称,请输入income_input.items()中的信息:
收入=信息[“工资”]
年龄=信息[“年龄”]
税=0.0
如果收入为50000和100000:
税=0.5*收入
#检查年龄

如果年龄使用上述嵌套字典结构,则可以通过以下方式访问员工的年龄:

income_input = {
    'Gerard': {'age': 50, 'salary': 120000},
    'Tom': {'age': 28,'salary': 60000},
    'Roos': { 'age': 25,'salary': 40000}
}

def calculate_tax(income_input):
    tax_dict = {}
    for name, info in income_input.items():
        income = info['salary']
        age = info['age']
        tax = 0.0
        if income <= 50000:
            tax = 0.3 * income
        elif income > 50000 and income <= 100000:
            tax = 0.4 * income
        elif income > 100000:
            tax = 0.5 * income

        # check for age
        if age <= 30:
            tax = tax/2.0

        tax_dict[name] = int(tax)

    return tax_dict

tax_dict = calculate_tax(income_input)
print(tax_dict)
就我个人而言,对我来说,使用每个员工只有两个字段的嵌套字典似乎有点过分了。您可以考虑使用元组代替,访问索引为0的年龄和带索引1的工资,例如:

income_input['Gerard']['age']  # returns 50 
income_input={“杰拉德”:(50120000),“汤姆”:(2860000),“公鸡”:(254万)}
对于收入投入中的k:
年龄、收入=收入\输入[k]

if(age)这段代码工作得很理想。非常感谢!我现在看到了逻辑顺序:)
income_input['Gerard']['age']  # returns 50 
income_input = {'Gerard': (50, 120000), 'Tom': (28, 60000), 'Roos': (25, 40000)}
income_input['Gerard'][0]  # returns 50
income_input = {"Gerard": (50, 120000), "Tom": (28, 60000), "Roos": (25,40000)}

for k in income_input:
    age, income = income_input[k]
    if (age <=30):
        if (income <= 50000):
            tax = (0.3*income)*0.5
        elif (income > 50000) and (income <= 100000):
            tax = (0.4*income)*0.5
        elif (income > 100000):
            tax = (0.5*income)*0.5 
        else:
            pass