python - 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:
解决方案
您需要在第二个 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!
推荐阅读
- c - 将 double 转换为 int 仅适用于常量
- angular - 将数据从 Observable 传递到 Ionic 5/Angular 中的全局变量
- javascript - Material UI v5 - 选项卡样式覆盖
- azure - 在 YAML 管道中将多个 Azure WebJobs 部署到单个 Azure AppService
- automation - 赛普拉斯无法从 window.__coverage__ 获取覆盖率报告
- java - Twitter API 使用 Oauth1.0 身份验证失败
- git - 如何将本地仓库链接到 Github 中的远程仓库
- python - 变量在函数中不起作用(python 3)
- xamarin.forms - 文件转换任务在 Xamarin.Forms 的发布管道中不起作用
- python - 将多个每日 NetCDF 文件中的变量组合到 Python 中该变量的一个年度时间序列数据文件中