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 Branch and Loop

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

My Solution - with Branching and Loops

Thoroughly enjoying building this little program. Thanks Craig Dennis for encouraging us to try things on our own and perhaps make our own interpretation the project.

To which end, I came up with this. I answered the ticket about notifying how many tickets remaining, and opened the opportunity of adding a new name/customer each time the loop goes through.

masterticket.py
TICKET_PRICE = 10

tickets_remaining = 100

#run this code until we run out of tickets

while tickets_remaining > 0:

    user_name = input("First, tell me your name >>  ")

    #output how many tickets are remaining using the tickets_remaining variable.

    print("Hello, {}".format(user_name))
    print("There are {} tickets available for you to buy right now, {}".format(tickets_remaining, user_name))

    #Gather the users name and assign it to a new variable

    # prompt the user by name and ask how many tickets they would like
    tickets_requested = input("Alright, {}! How many tickets would you like? >>   ".format(user_name))
    tickets_requested = int(tickets_requested)

    print("You requested {} tickets".format(tickets_requested))
    # calculate the price (number of tickets multiplied by the price) and assign it to a variable)
    total_price = (tickets_requested * TICKET_PRICE)

    # Output the price to the screen
    print("The total price will be £{}!".format(total_price))

    #Prompt user if they want to proceed  (Y/N) >>
    proceed = input("Proceed with your shop?  (Y/N)>>   ")

    #If they want to proceed
    if proceed.lower() == "y":
        #print out to to the screen to confirm sale
        # TODO: Gather credit card information and process it
        print("SOLD ({} tickets)".format(tickets_requested))

        #and then decrement the tickets remaining bt the number of tickets purchased
        tickets_remaining -= tickets_requested

        print("\nThere are now {} tickets remaining\n".format(tickets_remaining))
        print("\nNext Customer Please!\n")
    else:
    #otherwise

        #thank them by name
        print("Goodbye, {}! Thanks for using MasterTicket".format(user_name))

# Notify the user that tickets are sold out!
print("That's all. Tickets have sold out!")

3 Answers

Nice! :+1:

Hello Jonathan Grieve,

I copied and pasted your code along side my code so I could compare mine against yours. My code looks like a hot mess whereas your code looks clean and organized. Could you give me some tips on how to keep things organized etc. ? I would like to get in the habit of keeping it clean and organized and pared down to what absolutely needs to be included

Any input is appreciated.

import math

TICKET_PRICE = 10
tickets_remaining = 100

# Let everyone know that there is limited seating.
print("***| FYI |***" + "There are only 100 tickets available for this show.")

# Run this code continuously until we run out of tickets
while tickets_remaining > 0:


    # Gather the users name and assign it to a new variable.
    customer = input(f"Hello. What is your name? ").title()

    # Personalize the experience so that client feels welcomed by the brand.
    print(f"Welcome {customer}. I am Billy Bob.")

    # Prompt the user by name and ask them how many tickets they would like.
    ticket_quote = input(f"How many tickets would you like to purchase {customer}? ")
    ticket_quote = int(ticket_quote)

    # Calculate Price - number_of_tickets * 10.00 assign current variable to a new variable.
    doing_some_math = (ticket_quote * TICKET_PRICE)

    # Output the price to the screen.
    print("Amount due: $" + format(doing_some_math, ".2f"))

    # Prompt user if they want to proceed. Y/N?
    verifying_purchase = input(f"{customer}, would you like to proceed? Y/N ")
    verifying_purchase = str(verifying_purchase)


        # If they want to proceed 
        if verifying_purchase.lower() == "y":
            print(f"Excellent! We hope you enjoy the show {customer}.")
        elif verifying_purchase.lower() == "n":
            print(f"Times are tough {customer}. Perhaps next time. Cheers!")
        else:
            print(f"We appreciate your enquiry. Tak care {customer}.")

    # Print out to the screen "SOLD!" to confirm purchase
    print(f"SOLD! You have been charged: $" + format(doing_some_math, ".2f"))


    # decrement the tickets remaining by the number of tickets purchased
    tickets_left = tickets_remaining - int(ticket_quote)
    print(f"Let your family & friends know that there are only {tickets_left} available to purchase.")
    # Otherwise...
    print("")

# Notify the user that the tickets are sold out.
print("Unfortunately there are tickets available.")
Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

Have a look at the Python guides for laying out your code.

PEP 8 I think is the one you want to look for. These have been around for a long time and help us stay consistent in the way we write our code. https://www.python.org/dev/peps/pep-0008/

Your text editor help you set things like indentation which is typically 4 spaces

Jonathan Grieve Awesome! I will have a look at that.