我不明白这个(功能?)作品

2024-04-16 21:54:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python还很陌生,我正在读一本入门书。代码不是用英语写的,所以我尽力翻译,希望你们能理解。 在这个练习中,我们根据用户工资计算税款:

salary = float(input("Enter your salary to taxes calculation: "))
base = salary
taxes = 0

if base > 3000:
    taxes = taxes + ((base - 3000) * 0.35)
    base = 3000
if base > 1000:
    taxes = taxes + ((base - 1000) * 0.20)

我的问题是当输入大于3000时,例如,如果我以5000的薪水运行代码,结果将是1100。但是当我在计算器上做同样的计算时,结果是700,所以我在这里迷路了,有人能解释一下吗?你知道吗


Tags: to代码用户inputyourbaseiffloat
3条回答

接着是第二个函数

所以如果我把数字代入:

Salary = 5000
base = 5000
taxes = 0 

if 5000 > 3000:
   taxes = 0 + ((5000- 3000) * 0.35) # = 700
   base = 3000
if 3000 > 1000:
   taxes = 700 + ((3000 - 1000) * 0.20) # = 1100

好吧,让我们以5000为例来讨论一下

salary = float(input("Enter your salary to taxes calculation: "))
base = salary 
# base = 5000
taxes = 0

if base > 3000: # base is larger than 3000, so we enter the if statement 
    taxes = taxes + ((base - 3000) * 0.35)
    # taxes = 0 + ((5000 - 3000) * 0.35)
    # taxes = 0 + 700
    # taxes = 700
    base = 3000 # base is set to 3000
if base > 1000: # base was set to 3000 in the line above, so we enter the if statement
    taxes = taxes + ((base - 1000) * 0.20)
    # taxes = 700 + ((3000 - 1000) * 0.20), remember taxes is already 700 from above
    # taxes = 700 + 400
    # taxes = 1100

因为它是两个if语句,而不是一个if和一个else,所以当base设置为大于3000时,我们对这两个语句进行评估。我希望这有帮助。你知道吗

请注意,如果薪资为5000,则控件将转到两个if语句。第一个是700,第二个是400,所以答案是700+400。这也是有道理的,因为税的计算大多被划分在括号里,而不是工资的固定百分比。你知道吗

相关问题 更多 >