地点系统出口()终止cod

2024-09-26 22:51:24 发布

您现在位置: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 = 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键时,它将继续执行“计算账单”功能并计算/打印账单

实际结果:当在开始时按6时,在get\ U inputs中,在return语句中,在赋值之前引用局部变量“tax”


Tags: ofthecheckamountquantitychoicestotaltax
2条回答

你可以做循环,当你得到一个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)在代码开头,当汉堡的种类没有值时,只需退出代码,不提示用户再次输入。你知道吗

(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]$ 

相关问题 更多 >

    热门问题