Python Python Basics All Together Now Wrapping Up

Michael Cronk
Michael Cronk
5,552 Points

I did the project, I just feel like there is too much code. What is your opinion? Too much? It works perfectly.

SERVICE_CHARGE = 2

TICKET_PRICE = 10

tickets_remaining = 100


def service_charge_func(num_of_tickets):
    return (num_of_tickets * TICKET_PRICE) + SERVICE_CHARGE


while tickets_remaining >= 1:

    users_name = input("What is your name?  ")

    if tickets_remaining >= 1:

        print("There are {} tickets left...".format(str(tickets_remaining)))
    else:
        print("We have sold out of tickets, we're sorry for the inconvenience, {}".format(users_name.capitalize()))
    try:
        ticket_amount = int(input("How many tickets would you like, {}?  ".format(users_name.capitalize())))
        if ticket_amount < 1:
            raise ValueError(print("Your ticket amount has to be more than 0 and less than 101."))
        elif ticket_amount > 100:
            raise ValueError(print("Your ticket amount has to be more than 0 and less than 101."))
        elif ticket_amount > tickets_remaining:
            raise ValueError(print("There are only {} tickets remaining.".format(tickets_remaining)))
    except ValueError:
        print("What you entered is invalid... Please enter a whole number.")
    else:
        total_amount = service_charge_func(ticket_amount)
        print("Your sub-total is ${}".format(total_amount))
        proceed = input("Would you like to proceed? Y/N  ")
        if proceed.lower() == "y":
            # Gather payment information
            print("SOLD!")
            tickets_remaining -= ticket_amount
        elif proceed.lower() == "yes":
            # Gather payment information
            print("SOLD!")
            tickets_remaining -= ticket_amount
        elif proceed != str:
            print("What you entered is invalid... Please try again.")
        else:
            print("Okay, thank you for your time {}...".format(users_name.capitalize()))
else:
    print("All the tickets are sold out...")
Michael Cronk
Michael Cronk
5,552 Points

I don't know why the code posts like this, IDK how to fix it.

2 Answers

Steven Parker
Steven Parker
204,010 Points

One compacting technique you could use is to combine similar tests to avoid repeated code lines:

    if proceed.lower() == "yes" or proceed.lower() == "y":

And when posting code to the forum, use Markdown formatting to preserve the appearance, or share the entire workspace by making a snapshot and posting the link to it.

Michael Cronk
Michael Cronk
5,552 Points

I did that and already i reduced my code by 6 lines. Also thanks for telling me about the markdown format! Thank you!

Paul Kim
Paul Kim
1,660 Points

hm Im still new and was curious what your code would look like.. I think you don't need the elif ticket_amount > 100 part because you're pretty much already saying that by saying elif ticket_amount > tickets_remaining..