gpt4 book ai didi

python - if…elif…else中的语法错误

转载 作者:行者123 更新时间:2023-12-03 08:27:59 27 4
gpt4 key购买 nike

税收计算器

def computeTax(maritalStatus,userIncome):
if maritalStatus == "Single":
print("User is single")
if userIncome <= 9075:
tax = (.10) * (userIncome)
elif userIncome <= 36900:
tax = 907.50 + ((.15) * (userIncome - 9075))
elif userIncome <= 89350:
tax = 5081.25 + ((.25) * (userIncome - 36900))
elif userIncome <= 186350:
tax = 18193.75 + ((.28) * (userIncome - 89350))
elif userIncome <= 405100:
tax = 45353.75 + ((.33) * (userIncome - 186350))
elif userIncome <= 406750:
tax = 117541.25 + ((.35) * (userIncome - (405100)
else: # getting syntax error here
tax = 118118.75 + ((.396) * (userIncome - (406750))
return tax
else:
return "placeholder"

def main():
maritalStatusMain = input("Please enter your marital status (Single or Married)")
userIncomeMain = float(input("Please enter your annual income"))
finalTax = computeTax(maritalStatusMain,userIncomeMain)
print(finalTax)
main()

当我删除或添加语句时,语法错误似乎跳来跳去。

最佳答案

快速浏览一下周围的线条,发现缺少括号

...
tax = 45353.75 + ((.33) * (userIncome - 186350)) # <- two ending parens
elif userIncome <= 406750:
tax = 117541.25 + ((.35) * (userIncome - (405100) # <- one ending paren, plus extra paren around 405100
else:
...

除非已复制+粘贴到问题中失败,否则仅此而已

关于python - if…elif…else中的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39755012/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com