代码之家  ›  专栏  ›  技术社区  ›  Andrew

我需要帮助为这个问题编写代码

  •  0
  • Andrew  · 技术社区  · 2 年前

    编写一个计算用户年薪税的程序。它必须: 2、要求用户输入年薪 3、在屏幕上打印他们的税单

    他们遵循以下规则:

    0$18200零($0已纳税)

    18201美元45000美元18200美元以上每1美元19美分

    45001美元120000美元5092美元,超过45000美元每1美元加32.5美分

    180001美元及以上,51667美元加上180000美元以上每1美元45美分

    1 回复  |  直到 2 年前
        1
  •  0
  •   7shoe    2 年前

    此函数可以工作,并且不需要任何依赖项即可工作。

    def taxesDue(x:float):
        '''Function that takes in a person's yearly salary (unit: AUD) and returns the taxes due (unit: AUD)'''
        if(x <= 18200):
            return 0                         # lucky person
        elif(x <= 45000):
            return round(0.19*(x-18200), 2)
        elif(x<= 120000):
            return round(5092+0.325*(x-45000), 2)
        elif(x <= 180000):
            return round(29467+0.37*(x-120000),2)
        else:
            return round(51667+0.45*(x-180000)*0.45, 2)
    

    样本输出为

    taxesDue(16500)
    >0 
    
    taxesDue(18201)
    >0.19
    
    taxesDue(1e6) # scientific notation for 1 million (float)
    >217717.0
    

    1. 该函数的输入是以澳元表示的工资(可以是一个整数,如 20000 float 例如 20000.95 其中小数代表美分。因此,我将税款四舍五入到两位数 round(y, 2) 。如果输入工资始终为 int

    2. 说到 整数 .Python中的类型是动态的,因此 float:x 函数的参数列表中有语法糖(对于开发人员/用户来说很好看,但对代码的其余部分没有影响),以强调浮点数(工资)而不是字符串 str x=Hello IRS .请注意 整数 是的子集 所以 浮动 更一般。

    3. if / elif / else x <= 45000 ). 否则如果 其他的

    4. return 已达到。

    5. 评论,如 #lucky '''Function... 将进入文档字符串。反过来,开发人员可以在运行时检索它

    ?taxesDue
    

    enter image description here

    x = 475000       # or whatever salary you can think of
    print(taxesDue(x))