首页 > 解决方案 > Python 异常处理需要的建议

问题描述

我正在尝试将异常处理添加到成本和公司变量中。我需要一个例外来确保我的成本变量大于 0 美元。公司变量有一个例外,因此输入不为空。company 变量的异常有效,但我无法弄清楚如何分隔 if-else 语句。我对编程还是很陌生。

def main():
    print("This program will calculate the shipping cost based on "
          "the total purchase price,\n")

    while True:
        try:
            company = input('Please enter the company name: ')
            if company == "":
                raise ValueError("Please provide a valid company name.")
            break
        except ValueError as excpt:
            print(excpt)

    while True:
        try:
            cost = float(input("Please enter in the purchase price: "))
            if cost <= 0:
                raise ValueError("Please enter a value greater than $0.00.")
            break
        except ValueError as excpt:
            print(excpt)


        if cost <= 100.00:
            shipping = 10.0
        elif cost <= 300.00:
            shipping = 8.0
        elif cost <= 500.00:
            shipping = 5.0
        else:
            shipping = 0.0

        total = cost + shipping

        print("At a purchase price of ${:.2f}, the shipping cost will be ${:.2f},"
                  " with a final total of ${:.2f}.".format(cost, shipping, total))
        print('{}, thank you for shopping with us!'.format(company))

main() 

这是输出:

Please enter the company name: 
Please provide a valid company name.
Please enter the company name: ASX
Please enter in the purchase price: -5.99
Please enter a value greater than $0.00.
At a purchase price of $-5.99, the shipping cost will be $10.00, with a final total of $4.01.
ASX, thank you for shopping with us!
Please enter in the purchase price: 

标签: pythonexception

解决方案


您需要在第二个 while 循环之后修复第二个缩进。你正在闯入空虚。

新代码

def main(): 
    print("This program will calculate the shipping cost based on " "the total purchase price,\n")

    while True:
        try:
            company = input('Please enter the company name: ')
            if company == "":
                raise ValueError("Please provide a valid company name.")
            break
        except ValueError as excpt:
            print(excpt)
    while True:
        try:
            cost = float(input("Please enter in the purchase price: "))
            if cost <= 0:
                raise ValueError("Please enter a value greater than $0.00.")
            break
        except ValueError as excpt:
            print(excpt)


    if cost <= 100.00:
        shipping = 10.0
    elif cost <= 300.00:
        shipping = 8.0
    elif cost <= 500.00:
        shipping = 5.0
    else:
        shipping = 0.0

    total = cost + shipping

    print("At a purchase price of ${:.2f}, the shipping cost will be ${:.2f},"
            " with a final total of ${:.2f}.".format(cost, shipping, total))
    print('{}, thank you for shopping with us!'.format(company))
if __name__ == '__main__':
    main()

输出

This program will calculate the shipping cost based on the total purchase price,

Please enter the company name: apple
Please enter in the purchase price: -100
Please enter a value greater than $0.00.
Please enter in the purchase price: 100
At a purchase price of $100.00, the shipping cost will be $10.00, with a final total of $110.00.
apple, thank you for shopping with us!

推荐阅读