Python 在何处放置sys.exit()以终止代码
这是一个初级python程序,在这里我们可以看到一个菜单,用户可以从1-5和6中选择要退出的项。如果他们选择6,它将终止代码,不要问任何其他问题,也不要出示账单 我原以为将它放在“elif choice==6”会起作用,但随后它结束了整个代码,而没有考虑前面的其他选择Python 在何处放置sys.exit()以终止代码,python,Python,这是一个初级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 =
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
def get_inputs():
“获取用户选择的每个汉堡的输入,以及他们想要多少”
计数=0
quantity1=quantity2=quantity3=quantity4=quantity5=0
flag=True
而国旗:
检查\u choice=True
在选择时:
尝试:
choices=int(输入(“输入您想要的汉堡种类(1-5或6以退出):”).strip()
如果我从预期输出中了解到选项,您希望在以下场景中退出代码:-
(1) 在代码的开头,当汉堡的种类没有价值时,只需退出代码,而不提示用户再次输入
(2) 在burger count中保存一些值后,如果用户按6,则也不应向用户询问价格计算逻辑
如果我的理解是正确的,那么您应该按照以下方式更新代码:-
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:
check_choices = False
flag = False
import sys
sys.exit()
输出如下:-
(.test) [nikhilesh@pgadmin]$ python3 1.py
Enter kind of burger you want(1-5 or 6 to exit): 1
Enter quantity of burgers wanted: 2
Enter kind of burger you want(1-5 or 6 to exit): 6
(.test) [nikhilesh@pgadmin]$ python3 1.py
Enter kind of burger you want(1-5 or 6 to exit): 6
(.test) [nikhilesh@pgadmin]$ python3 1.py
Enter kind of burger you want(1-5 or 6 to exit): 1
Enter quantity of burgers wanted: 4
Enter kind of burger you want(1-5 or 6 to exit): 6
(.test) [nikhilesh@pgadmin]$
你可以做循环,当你得到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')
#价格示例。
单价={
1:7.89#大盘价
2:11.00,卑诗省价格
3: 9.50,
4: 15.85,
5: 21.00
}
职工税=0.2
def calcbill(单位、可征税、单价=单价、税率=员工税):
小计=0
以单位表示的u:
小计+=单价[u]
如果可编辑:
税额=小计*(税率/100)
其他:
税额=0
报税表(小计+税额、税额)
条目=[]
打印(“输入您想要的汉堡种类(1-5或6退出):”)
尽管如此:
尝试:
choice=int(输入(“下一个汉堡是什么?”)
如果选项==6:
打破
elif 0
对不起,我不明白你的问题。您是否想在6点按一下按钮立即终止程序?Hille,他的exit
意思是“只需完成输入”。只要编写一个函数来完成所有的处理(如果可能的话),并调用sys.exit
@Hille,就像我在开始时按6键一样,它会终止程序,否则它会继续请求用户输入。当他们按下6键时,它将退出循环并继续下一个函数,在那里它将计算价格。希望我的措辞是正确的,有没有一种方法,如果他们在汉堡计数中保存了一些值,用户按6,它仍然会进行价格计算?这是我的老师希望我们做的,但我不知道该把sys.exit()放在哪里