gpt4 book ai didi

python - 在哪里放置 sys.exit() 以终止代码

转载 作者:太空宇宙 更新时间:2023-11-04 11:15:18 25 4
gpt4 key购买 nike

这是一个初学者 python 程序,我们有一个菜单,用户可以从 1-5 和 6 中选择要退出的项目。如果他们选择 6,它将终止代码,不要再问任何其他问题,也不会显示帐单。

我认为将它放在“elif choice == 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。是一种非常残酷的终止程序的方式。通常最好将终止决定委托(delegate)给应用程序中最外层的函数。最好让程序在到达程序末尾时自然终止。

您可能会使用 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 - 在哪里放置 sys.exit() 以终止代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57157552/

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