Should I be using an if statement or a loop?

Viewed 57

It's me, your friendly Python noob!

I'm working on this program and I'm trying to figure out how to get the program to stop if the number exceeds a certain threshold OR if the user enters anything other than a number to loop back around and try again.

Here is what I have so far:

def ticket_a_sales_calculation(section_a_ticket_purchased):
    section_a_sales = section_a_ticket_purchased * 20
    return section_a_sales

def ticket_b_sales_calculation(section_b_ticket_purchased):
    section_b_sales = section_b_ticket_purchased * 15
    return section_b_sales

def ticket_c_sales_calculation(section_c_ticket_purchased):
    section_c_sales = section_c_ticket_purchased * 10
    return section_c_sales

def total_sales_calculation(ticket_a_sale, ticket_b_sale, ticket_c_sale):
    total_sales = ticket_a_sale + ticket_b_sale + ticket_c_sale
    return total_sales

def sales_report(ticket_a_sales, ticket_b_sales, ticket_c_sales, sales_totals):
    print("Ticket A Sales: $" + format(ticket_a_sales, ",.2f"))
    print("Ticket B Sales: $" + format(ticket_b_sales, ",.2f"))
    print("Ticket C Sales: $" + format(ticket_c_sales, ",.2f"))
    print("Total Sales: $" + format(sales_totals, ",.2f"))

def main():
    ticket_a_purchased = int(input("Please enter the amount of tickets sold for section A: "))
    if ticket_a_purchased > 300:
        print("Number exceeds amount of available seats")

    ticket_b_purchased = int(input("Please enter the amount of tickets sold for section B: "))
    if ticket_b_purchased > 500:
        print("Number exceeds amount of available seats")

    ticket_c_purchased = int(input("Please enter the amount of tickets sold for section C: "))
    if ticket_c_purchased > 200:
        print("Number exceeds amount of available seats")

    ticket_a_sales = ticket_a_sales_calculation(ticket_a_purchased)
    ticket_b_sales = ticket_b_sales_calculation(ticket_b_purchased)
    ticket_c_sales = ticket_c_sales_calculation(ticket_c_purchased)
    total_sales = total_sales_calculation(ticket_a_sales,ticket_b_sales,ticket_c_sales)
    sales_report(ticket_a_sales, ticket_b_sales, ticket_c_sales, total_sales)

main()

The if statements in main() are printing what I want, but what I'm trying to do is to have the program stop under certain conditions. I know a while or for loop would help, but I can't figure out how to set it up.

What I ended up trying next was this:

    seats_a = True
    while True:
        if seats_a > 300:
            print("Number exceeds amount of available seats")
        elif seats_a < 300:
            continue

That didn't work at all.

At this point I'm at a loss. Would anyone be able to nudge me in the right direction? I feel like the if statements are not the right way to go. I feel using a while loop would work better and give me the result I'm looking for.

God, I hope that makes sense.

Thanks folks!

3 Answers

This idiom should work for this scenario:

while True:
    ticket_a_input = input("Please enter the amount of tickets sold for section A: ")
    if not ticket_a_input.isdigit():
        print("Ticket amount must be a number")
        continue
    if int(ticket_a_input) > 300:
        print("Number exceeds amount of available seats")
        continue
    ticket_a_purchased = int(ticket_a_input)
    break

What we do here is loop infinitely, unconditionally, using while True. Then, depending on what the user enters,

  • If the user enters not a number, then we print an error message and jump back to the start of the loop with continue
  • Otherwise, if the user enters a number greater than 300, print a different error message and restart the loop
  • If the user's input is valid and hasn't hit either of those, then set ticket_a_purchased and break out of the loop.

This type of idiom works for most "conditional inputs", where the input may or may not be valid and you want to keep asking until it is, but it's rarely the best solution for other scenarios. Whenever reasonably possible you want to try to have a while loop with an actual condition that can be satisfied on its own. If you do this, you need to be very careful that every possible path through the loop is covered one way or another, or you could find yourself in an unbreakable loop.

You're on the right track. A while loop is definitely the way to go, after all, you want to do something while some condition is true (a threshold is not yet exceeded and the user hasn't entered illegal input)

However, you tried continue - this causes the loop to stop the current iteration and start with the next iteration of the loop. That's not what you want, you seem to want to break which also stops the current iteration, but then continues the program outside the current loop (or more generally, the current block).

Also, you assign True to seats_a but then continue to use the same variable to hold what its name suggests, a number.

you need break which will immediately stop execution of the rest of the code and your choice of elif is way to go. You will be able to check all conditions and with break, you are sure that your loop will stop when at least one of the seats type is not available anymore. In that case you do not even need elif combined with continue for the situation when there are still available seats. That is Because what comes after if, will not be executed. Moreover, in your print, I would indicate which seats are not available (a, b or c)

while True:
    if seats_a > 300:
        print("Number exceeds amount of available 'a' seats")
        break
    elif ticket_b_purchased > 500:
        print("Number exceeds amount of available 'b' seats")
        break

The loop will stop only when at least one type of the seats is no longer available.

Related