0

I have created a list of 5 checkbuttons, displaying 5 different pizzas to choose. When one or more buttons are clicked it will calculate the total price of pizzas. I have a function with for loop and if statement excuting the price of pizza when clicked. How can I make the output display the total price of pizzas if one or more pizzas are clicked but also display "you have ordered no pizza" when none have been clicked? In my if statement it is displaying the total price of pizzas clicked but also displaying "you have ordered no pizza" due to the other choices not being clicked. I need it to display "you ordered no pizza" when only no buttons have been clicked.

def total():
    top = Toplevel()
    tot = 0
    name = entry1.get()
    address = entry2.get()
    for pizza,var in zip(pizzas,var_list):
        if var.get() != 0:
            tot = tot + var.get()
            label = Label(top, text="Your total cost of pizza is ${}\nShipping to {},\n at this address: {}".format(tot, name, address), font='helvetica, 32').grid(row=9, column=0)
        else:
            label1 = Label(top, text='you ordered no pizza').grid(row=11, column=0)
    button = Button(top, text='Exit', command=top.destroy).grid(row=10, column=0)

1 Answer 1

1

You need to check whether tot > 0 after the for loop to determine which message to be shown:

def total():
    top = Toplevel()
    tot = 0
    name = entry1.get()
    address = entry2.get()
    for pizza,var in zip(pizzas, var_list):
        price = var.get()
        if price != 0:
            tot += price

    if tot > 0:
        Label(top, text="Your total cost of pizza is ${}\nShipping to {},\n at this address: {}".format(tot, name, address), font='helvetica, 32').grid(row=9, column=0)
    else:
        Label(top, text='you ordered no pizza').grid(row=11, column=0)

    Button(top, text='Exit', command=top.destroy).grid(row=10, column=0)

Note that assigning Label(...).grid(...) (always None) to a variable is meaningless.

Sign up to request clarification or add additional context in comments.

1 Comment

little detail regarding "always". one can create own customary class named Label that inherits from Label and change grid to return the widget itself... but I am just being too pedantic right now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.