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

If Statement

I encounter a problem, it always output to the "else" even if I type "Y or y" please check my code, Thanks in advance.

TICKET_PRICE = 10

tickets_remaining = 100

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

user_name = input("Please enter you're name :")

ticket_prompt = int(input("Hi {}, how many tickets would you like? ".format(user_name)))

ticket_calc = ticket_prompt * TICKET_PRICE

print("The total price of ticket you would like to buy is ${}.".format(ticket_calc))

user_prompt = input("Would you like to proceed? Y/N ")

if user_prompt.lower() == "Y":
    print("SOLD!")
else:
    print("Thanks, {}.".format(user_name))

1 Answer

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,254 Points

Try changing the value of the condition you're testing for to a lowercase "y". It's not picking up the "capital Y" of your expression because Python is lowercasing it and always assumes it will be a lowercase letter.

This way, once the expression is changed a user can safely type a "Y" or a "y" and you will always get the response you want.

By the way, I also fixed your forum code with "MarkDown". Checkout the Markdown Basics course in the library to help you post code to the forum with markdown! :-)

Sir Jonathan it works, I've changed the value of condition to lowercase "y". Thanks man :)