需要帮助,被python代码困住了

时间:2019-01-21 15:19:28

标签: python

任何人都可以告诉我代码是什么问题,因为我是python的新手,我也不知道。

问题: 一个城镇包含5000 房屋。每个房主必须根据房屋价值缴税。超过20万美元的房屋缴纳其税额的2%,超过10万美元的房屋缴纳其税额的1.5%,而超过5万美元的房屋缴纳其税额的1%。所有其他人不缴税。

cost = int(input("What is the cost of your house?"))
tax = (cost/100)
if (cost) < 0:
    print("Error")
elif (cost) < 50000:
    tax == (cost/100)*0
    print("You don't have to pay tax")
elif (cost) > 50001 and (cost) < 100000:
    tax == (cost/100)*1
    print("You have to pay 1% tax") 
elif (cost) > 100001 and (cost) < 200000:
    tax == (cost/100)*1.5
    print("You have to pay 1.5% tax")
elif (cost) > 200001:
    tax == (cost/100)*2
    print("You have to pay 2% tax")

1 个答案:

答案 0 :(得分:0)

这样的事情,避免了重复的代码,并且对用户输入进行了一些错误处理:

(此外,您的数学有点差,因为根本没有处理50,000、100,000和200,000,例如:(cost) < 50000(cost) > 50001留下了50,000缺口,因为两者都不存在小于50,000或大于50,001。

while True:
    try:
        cost = int(input("What is the cost of your house?"))
        tax = None

        if cost <= 50000:
            tax = 0
        elif 50000 < cost <= 100000:
            tax = 1
        elif 100000 < cost <= 200000:
            tax = 1.5
        elif cost > 200000:
            tax = 2

        if tax == 0:
            print("You don't have to pay tax")
        else:
            print("You have to pay {}% tax of ${:.2f}".format(tax, (cost/100)*tax))
        break
    except Exception as e:
        print("Bad value entered,", e)