Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics All Together Now Wrapping Up

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...")

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

2 Answers

Steven Parker
Steven Parker
231,248 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.

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

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..