mardi 20 octobre 2020

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!

Aucun commentaire:

Enregistrer un commentaire