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 trialRandolph Davidson
1,117 PointsElse statement causing an syntax error. though the issue is on the line above.
Hello again Fellow learners. I am having an issue with the last part of this challenge. it says there is a syntax error on my else statement. However, it is really the tickets_remaining -= requested_tickets. When I remove this, the program works though nothing reduces and it just loops forever. Not sure what the issue is. Been reading over it all day. Kinda at a lost. Any ideas? I'm grateful for the help.
TICKET_PRICE = 10
tickets_remaining = 100
there are {} tickets remaining
while tickets_remaining >= 1:
print("there are {} tickets remaining".format( tickets_remaining))
#Gather the user name nd assign it to a new ver
Guest =input("Hello there, May I have your name please? ")
#prompt the user by name and ask how many tickets they would like
requested_tickets = int(input("Thanks {}, please tell me how many tickets are you interested in today? ".format(Guest)))
total = requested_tickets * TICKET_PRICE
#Output the total price to the screen
print("All done! your amount due today is {} ".format(total))
#prompt user if they would like to proceed Y/N
input("{} would you like to proceed with your purchase today? Yes/No ".format(Guest))
proceed.lower()== "y"
#If yes, print out to screen "SOLD!" to confirm
if proceed:
print("SOLD! to {}".format(Guest))
#Reduce number on remaining Tickets by the number of Tickets bought.
tickets_remaining -= requested_tickets
else:
print("Thanks{}, maybe next time".format(Guest))
print("Sorry all the tickets are sold out")
#Otherwise thank user by name.
1 Answer
Jennifer Nordell
Treehouse TeacherHi there, Randolph Davidson ! It looks like you're doing well. Yes, the issue is above the else
. The problem here is that you have an if
statement. Inside the if
statement you have a print
. However, the very next line is not indented inside the if
. It lines up with the if
. So when you say else
it's unexpected because there's a line between the if
and the else
that does not fall inside the if
.
Right now you have:
if proceed:
print("SOLD! to {}".format(Guest))
#Reduce number on remaining Tickets by the number of Tickets bought.
tickets_remaining -= requested_tickets
else:
But that should be:
if proceed:
print("SOLD! to {}".format(Guest))
#Reduce number on remaining Tickets by the number of Tickets bought.
tickets_remaining -= requested_tickets
else:
Any and all lines that come between the if
and the else
must be inside the if
. Otherwise, Python will consider the else
"unexpected".
Hope this helps!
Randolph Davidson
1,117 PointsJennifer Nordell You are awesome. As many times as I went over it I did not notice that lol. Thanks so much. Something to keep in mind. I have a second question for you if you'd care to have a look? I'll post it below.
Randolph Davidson
1,117 PointsI'll start by saying I noticed in the video it does not seem to address if the user should attempt to order more than the 100 tickets available. At least in my attempt at the code When I order more than 100, such as 101 it runs. In the code I have tried to stop that from happening and I get the intended result however, it doesn't print what I told it to print. Rather, it prints out "there are 100 tickets remaining message. Right result wrong message. Any tips?
TICKET_PRICE = 10
tickets_remaining = 100
there are {} tickets remaining
while tickets_remaining >= 1:
print("there are {} tickets remaining".format( tickets_remaining))
#Gather the user name nd assign it to a new ver
Guest =input("Hello there, May I have your name please? ")
#prompt the user by name and ask how many tickets they would like
requested_tickets = int(input("Thanks {}, please tell me how many tickets are you interested in today? ".format(Guest)))
total = requested_tickets * TICKET_PRICE
if requested_tickets > 100: print("Sorry, that exceeds the number of tickets available")
#Output the total price to the screen
print("All done! your amount due today is {} ".format(total))
#prompt user if they would like to proceed Y/N
proceed=input("{} would you like to proceed with your purchase today? Yes/No ".format(Guest))
#If yes, print out to screen "SOLD!" to confirm
if proceed.lower()=="y": print("SOLD! to {}".format(Guest))
#Redue number on remaining Tickets by the number of Tickets bought.
tickets_remaining -= requested_tickets
else: print("Thanks {}, maybe next time".format(Guest))
print("Sorry all the tickets are sold out")
#Otherwise thank user by name.
Randolph Davidson
1,117 PointsRandolph Davidson
1,117 Pointsalso sorry for not posting the snapshot. I cant take one for some reason. I already sent an email to support