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

Eliajh Mais
Eliajh Mais
2,000 Points

says tickets sold out when there is tickets

Everything in my code is running perfectly it's just when you for example buy 3 tickets with 100 tickets still remaining it says Sorry the tickets are all sold out!!! :(
There are 97 tickets remaining. the same thing happens when you do not answer "y" , It says Thank you anyways,
Sorry the tickets are all sold out!!! :(

My Code: TICKET_PRICE = 10

tickets_remaining = 100

Run this code continuously until we run out of tickets

while tickets_remaining >=1:

#Output how many tickets are remaining using the tickets_remaininng variable

print("There are {} tickets remaining.".format(tickets_remaining))

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

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

# Calculate the price (number of tickets multiplied by the price) and  assign it to a variable


Amount_due=num_tickets*TICKET_PRICE

# Output to the price to the screen
print("The total due is ${}".format(Amount_due))
#Prompt user if they want to proceed. Y/N?
proceed = input("Do you want to proceed with your purchase? Y/N")

# If they want to proceed
if proceed.lower()=="y":
    #print out to the screen "SOLD!" to confirm purchase
    # TODO: Gather credit information and process it.
     print("SOLD!")
    # and then decrement the tickets remaining by the number of tickets purchased
     tickets_remaining -= num_tickets
#Otherwise....
else:
    # Thank them by name
    print("Thank you anyways,{}!".format(User_name))

# Notify user that the tickets are sold out
print("Sorry the tickets are all sold out!!!  :(")
sarvienn thevendran
sarvienn thevendran
734 Points

that's because your print function on the last line is indented in such a way that it is commanded to print that statement anyway.

add it like this : on the first section:

if tickets_remaining > 0: input(proceed input) elif tickets_remaining == 0 : print("sorry we have sold out")

adding this to the first phase should help your code run in a way it checks first before taking in all the input.

if youre still not sure please let me know, I will be more than happy to elaborate more

1 Answer

ABDELJALIL AOURAGHE
ABDELJALIL AOURAGHE
4,038 Points

assuming that your code begins from " TICKET_PRICE = 10 ". I think you need to intend all the code from

#Output how many tickets are remaining using the tickets_remaininng variable

print("There are {} tickets remaining.".format(tickets_remaining))

to

else:
    # Thank them by name
    print("Thank you anyways,{}!".format(User_name))

here is my code, and it works fine:

TICKET_PRICE = 10

tickets_remaining = 100  

# run this code continuously until we run out of tickets
while tickets_remaining >= 1:
    # output how many tickets are remaing using the tickets_remaining varible
    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
    num_tickets = input("how many tickets would you like?, {} ".format(name))
    num_tickets = int(num_tickets)

    # calcultate the price (number of tickets multiplied by the price) and assign it to a variable
    amount_due = num_tickets * TICKET_PRICE

    # output the price to the screen
    print(" the total is {}".format(amount_due))

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


    # if they want to proceed
    if should_proceed.lower()=="y":
        # print out to the screen "SOLD!" to confirm purchase
        # TODO: gather credit card information and process it,
        print("SOLD!")

        # and then decrement the tickets remaining by the number of tickets remaining
        tickets_remaining -= num_tickets

    # otherwise...
        #thank them by name
        print("thank you {}".format(name))


# notify the user that the tickets are sold out
print("the tickets are sold out!")