Jay Rosenthal Jay Rosenthal - 14 days ago 6
Python Question

Is there a way to capitalise specific letters in a value in python?

For a piece of homework, a part of the code involved asking the user to choose between 3 inputs. To make sure the code can accept the options in any format, I used .lower() as shown below.

while True:
RequestTea = input("What tea would you like? English Breakfast, Green Tea or Earl Grey? ").lower()
if RequestTea.lower() not in ('earl grey','english breakfast','green tea'):
print("We do not offer that unfortunately, please choose again.")
else:
break


In order to confirm the users choice I used three different prints depending on the value,

if RequestTea == 'earl grey':
print("Earl Grey Tea selected!")
if RequestTea == 'green tea':
print("Green Tea selected!")
if RequestTea == 'english breakfast':
print("English Breakfast Tea selected!")


In order to reduce this to two lines of code, I tried to make it print ('RequestTea'," selected") however I wanted the tea name to be displayed with first letters in the upper case, and the input was .lower().
I would like the tea name (Request Tea) to be displayed as a title, and then the "selected" to be displayed in lower case.

Thank you very much.

Answer

There is another way to do that in the print statement:

offered_tea = ('earl grey', 'english breakfast', 'green tea')
while True:
    RequestTea = input("What tea would you like? English Breakfast, Green Tea or Earl Grey? ")
    RequestTea_Lower = RequestTea.lower()
    if RequestTea_Lower in offered_tea:
        print(RequestTea_Lower.title(),' selected!')
    else:
        print("We do not offer that unfortunately, please choose again.")
Comments