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

Maba Bah
Maba Bah
2,744 Points

tickets-remaining is always 100

TICKET_PRICE = 10 tickets_remaining = 100

Keep runnin code so long as there are tickets for sale

while tickets_remaining >= 1:

Output how many tickets are remaining using the tickets_remaining variable

print("There are only {} tickets left!".format(tickets_remaining))

#Gather the user's name and assign it to a new variable
username = input("Hello! what is your name? ")

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

#Calcualte the price and assign it to a variable,
total = tickets_requested * TICKET_PRICE


# Output the price to screen
print("Your total {} is ${}".format(username,total))

#Prompt user if they want to proceed Y/N

#If they want to proceed
#print out to screen SOLD!
#and then decrement the tickets remaining
Y = "Y"
N = "N"
confirm = input("Would you like to proceed? ")

if confirm == Y:
        print("SOLD!")
        tickets_remaining -= tickets_requested

else: print("Thank you {}, Have a nice day!".format(username))

print("Tickets are SOLD OUT!")

Steven Parker
Steven Parker
231,248 Points

When posting code, use Markdown formatting to preserve the code's appearance and prevent special characters from being consumed. An even better way to share code and make your issue easy to replicate is to make a snapshot of your workspace and post the link to it here.

And the tech term for making the program accept answers of either case is "case insensitivity".

4 Answers

Dantee Fluellen
Dantee Fluellen
1,499 Points

YES YES YES YES i figured it out. Im happy with myself. It took me like 20 mins to go over and over it but i go it. ok

So in your code where it says if confirm == Y: thats not right. it should be if confirm == "Y": then after that i went back to try it and it still didnt work because you all forgot to tell the program where to recognize what the user puts in. i dont know they tech term for it but it it should look like this if confirm.lower() == "y" after you change that everything should be right. Also after would you like to process you should tell them what to put in. Y/N

edit: i just fix your problem but now im having the same problem. omg lol. but all i did was take out the commets everything is still the same. The program works fine with the comments in but dont with them out. Im confused.

Steven Parker
Steven Parker
231,248 Points

:star: for managing to wade through the unformatted code. :wink:

Dom Ss
Dom Ss
4,339 Points

Here is mine Workspace: https://w.trhou.se/doz1vwq3eh

What I fail to understand is the global scope to local scope. How do I update the variable remaining_tickets. I was thinking that I should just return from my function remaining_tickets, but that did not work.

im having the same issue, I've even been reading peoples answer line by line comparing it with mine and no matter how much i run it the "if" statement never runs and after i input Y is just loops back towards the beginning with nothing subtracted.

TICKET_PRICE = 10

tickets_remaining = 100

Run this code until we run out of tickets

while tickets_remaining >= 1: # want to output the remaining ticket using tickets_remaining

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


# We would need to find out their name and make it into a variable

name = input("Hi there! Welcome to MasterTicket! Whats your name?   ")

# Prompt the user by name and ask them how many tickets they would like
number_of_tickets = input("Hi {}! How many tickets would you like to purchase?  ".format(name)) 

number_of_tickets = int(number_of_tickets)

# Calculate the price (number of tickets * ticket price) assign to variable

total_due = number_of_tickets * TICKET_PRICE

# Output the price to the screen

print("Your total will be ${}!".format(total_due))

# Prompt user is the want to proceed? y/n
proceed_out = input("Would you like to proceed? Y/N ")

# If they want to proceed 
if proceed_out.lower() == "Y" :
# print out to the screen "sold!"
#TODO: Gather credit card info
    print("Sold!")
# reduce tickets remaining by the number purchased
    tickets_remaining -= number_of_tickets
    print("There are {} tickets remaining!".format(tickets_remaining))

# otherwise.....
else:
    print("Thank you for stopping by {}!".format(name))
# Thank them bname

# Notify User that tickets are sold out  

print("Sorry! We are sold out!")

Kevin LG
Kevin LG
5,641 Points

Ken Funakoshi: I know it's a bit late, but while I was going through this exercise again and checking the community questions for pointers and alternative code to try, I found your problem submission without a solution. After reviewing your code, I found that your if statement, if proceed_out.lower() == "Y":, prevents the rest of the block from running because the lower method always makes your letter Y input into lowercase y which will never be equal to the capital Y you've set for comparison. Alternatively, if the upper method was used, your code would have run fine.

your code:

 # If they want to proceed
    if proceed_out.lower() == "Y":
        # print out to the screen "sold!"
        # TODO: Gather credit card info
        print("Sold!")

revised code:

 # If they want to proceed
    if proceed_out.lower() == "y": # <-------here is what needed to be changed
        # print out to the screen "sold!"
        # TODO: Gather credit card info
        print("Sold!")

Hope you were able to find the bug before I posted this.