python - 在哪里放置 sys.exit() 以终止代码
问题描述
这是一个初学者 Python 程序,我们在其中得到一个菜单,用户可以从 1-5 和 6 中选择要退出的项目。如果他们选择 6,它将终止代码,不问任何其他问题,也不显示账单。
我认为将它放在“elif 选择 == 6”处会起作用,但它会结束整个代码而不考虑其他先前的选择
def get_inputs():
'''get input of each of the burger choices of the user and how much did they want'''
count = 0
quantity1 =quantity2=quantity3=quantity4=quantity5 = 0
flag = True
while flag:
check_choice = True
while check_choice:
try:
choices=int(input("Enter kind of burger you want(1-5 or 6 to exit): ").strip())
if choices <=0:
print("Enter a positive integer!")
else:
check_choice = False
except:
print("Enter valid numeric value")
check_quantity = True
while check_quantity and choices != 6:
try:
quantity = int(input("Enter quantity of burgers wanted: "))
if quantity <=0:
print("Enter a positive integer!")
else:
count +=1
check_quantity = False
except:
print("Enter valid numeric value")
if choices == 1:
quantity1 = quantity
elif choices == 2:
quantity2 = quantity
elif choices == 3:
quantity3 = quantity
elif choices == 4:
quantity4 = quantity
elif choices == 5:
quantity5 = quantity
elif choices == 6:
flag = False
check_staff = True
while check_staff and count !=0:
try:
tax = int(input("Are you a student? (1 for yea/0 for no)"))
check_staff = False
except:
print("Enter 1 or 0 only")
return quantity1,quantity2,quantity3,quantity4,quantity5,tax
def compute_bill(quantity1,quantity2,quantity3,quantity4,quantity5,tax):
'''calculate the total amount of the burgers and the total price of the purchase'''
total_amount = tax_amount = subtotal = 0.0
student_tax = 0
subtotal = (quantity1 * DA_PRICE) + (quantity2 * BC_PRICE) + (quantity3 * MS_PRICE) + (quantity4 * WB_PRICE) + (quantity5 * DCB_PRICE)
if(tax == 0):
tax = float(STAFF_TAX)
tax_amount = subtotal *(tax/100)
total_amount = subtotal + tax_amount
elif(tax == 1):
total_amount = subtotal+student_tax
return tax_amount, total_amount, subtotal
预期:当启动程序并按 6 时,它会终止,不问任何其他问题,也不显示账单
预期:代码将获取用户的输入,然后当按下 6 时,它将继续执行 comput_bill 函数并计算/打印账单
实际结果:开始按6时,get_inputs中,return语句中,赋值前引用了局部变量“tax”
解决方案
你可以只做循环,当你得到一个 6 时,你退出循环。如果没有输入,则跳过学生检查和账单计算。
这比尝试使用标志变量来检查是否应该打印要干净得多。
使用sys.exit
. 是一种相当残酷的方式来终止你的程序。通常最好将终止决定委托给应用程序中的最外层函数。到达程序结束时让程序自然终止也更好。
您可能会使用sys.exit
诸如不正确的命令行参数之类的东西。
# example prices.
unitprices = {
1: 7.89, # DA_PRICE
2: 11.00, # BC_PRICE
3: 9.50,
4: 15.85,
5: 21.00
}
STAFF_TAX = 0.2
def calcbill(units, istaxable, unitprices=unitprices, taxrate=STAFF_TAX):
subtotal = 0
for u in units:
subtotal += unitprices[u]
if istaxable:
tax_amount = subtotal * (taxrate / 100)
else:
tax_amount = 0
return (subtotal + tax_amount, tax_amount)
entries = []
print("Enter kind of burger you want(1-5 or 6 to exit): ")
while True:
try:
choice = int(input("what is the next burger? "))
if choice == 6:
break
elif 0 < choice < 6:
entries.append(choice)
else:
print('invalid choice')
except:
print('not a number')
if entries:
while True:
s = input('Are you a student? ').lower()
if s in ('y', 'yes', 'true'):
isstudent = True
break
elif s in ('n', 'no', 'false'):
isstudent = False
break
else:
print('not a valid value')
total, tax = calcbill(entries, not isstudent)
print(f'the bill was ${total:.2f} which includes ${tax:.2f} tax')
推荐阅读
- python - 获取两个excel文件之间的差异
- apache-spark - 将列添加到 df1 中不存在但存在于 df2 中的数据框
- http - 如何在 Ubuntu 16.04 上使用 python-certbot 保护 Nginx
- dynamics-crm-2011 - Dynamics 365 - 拆分解决方案
- coinbase-api - 我在哪里可以找到 coinbase 网站上使用的 assets api?
- angularjs - 使用 Webpack 4 捆绑 Angular.JS 模板
- ios - 调整容器视图的高度以匹配嵌入式 UITableViewController
- php - FPDF 将文件保存在服务器 QNAP(NAS) 上的文件夹中?
- java - 重用连接的套接字(java)
- node.js - 节点查找不返回聚合数据