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

Temi Folorunsho
Temi Folorunsho
2,849 Points

Branch and loop on tickets_remaining exercise

In the while loop, the codes still runs if the num_tickets is zero, is it supposed to run after we already specified it as >=1 ?

TICKET_PRICE = 10

tickets_remaining = 100

Run this code continuosly until we run out of tickets

while tickets_remaining >= 1:
# Output how many tickets are remaining using the tickets_remaining variable. print("There are {} tickets left".format(tickets_remaining))

# Gather the user's name and assign it to a new variable.
name = input("What is your name?  ")

# Prompt the user by name and ask how many tickets they would like.
number_of_tickets = int(input("Hey {}, how many tickets would you like?".format(name)))

# Calculate the price(number of tickets multiplied by the ticket_price) and assign it to variable
price = number_of_tickets * TICKET_PRICE 

# Output the price to the screen
print("The ticket costs ${}".format(price))

# Prompt user if they want to proceed Y/N?
should_proceed = input("Do you want to proceed? Y/N  ".format(name))
# if they want to proceed
if should_proceed.lower() == "y":
    # print out to the screen SOLD! to confirm purchase
    print("SOLD!")
    # and the decreament of the tickets remaining by the number of tickets purchased
    tickets_remaining -= number_of_tickets 
# Otherwise....
else:
    # Thank them by name
    print("Thank you {} for patronizing us".format(name))

Notify the user that tickets are sold out

print("Hey {} the tickets are sold out".format(name))

1 Answer

christopher diaz
christopher diaz
2,364 Points

Hey Temi,

Looking over your code, I noticed you did not add a couple of items. I am pasting your code and including the item that I am referring to, as well as commenting on them with my name next to it.

TICKET_PRICE = 10 tickets_remaining = 100

To trigger the remaining tickets available, you need the while loop - Chris Diaz

while tickets_remaining >= 1:

# The code in line 7 outputs the latest number of tickets available - Chris Diaz
print("There are {} tickets remaining".format(tickets_remaining))

# Gather the user's name and assign it to a new variable.
name = input("What is your name?  ")

# Prompt the user by name and ask how many tickets they would like.
number_of_tickets = int(input("Hey {}, how many tickets would you like?".format(name)))

# Calculate the price(number of tickets multiplied by the ticket_price) and assign it to variable
price = number_of_tickets * TICKET_PRICE

# Output the price to the screen
print("The ticket costs ${}".format(price))

# Prompt user if they want to proceed Y/N?
should_proceed = input("Do you want to proceed? Y/N  ".format(name))

# if they want to proceed
if should_proceed.lower() == "y":
    # Print out to the screen SOLD! to confirm purchase
    print("SOLD!")
    # and the decrement of the tickets remaining by the number of tickets purchased
    tickets_remaining -= number_of_tickets
    # Otherwise....
else:
    # Thank them by name
    print("Thank you {} for patronizing us".format(name))

This gets triggered once the application recognizes it no longer has tickets - Chris Diaz

print("Sorry the tickets are all sold out!!!")