Skip to content
Advertisement

Should I be using an if statement or a loop?

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!

Advertisement

Answer

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.

Advertisement