-2

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.

  • 1
    Try `print(RequestTea.title(), 'selected!')` – vaultah Nov 19 '16 at 13:59
  • I'm pretty sure I have seen the exact question and code yesterday by another user.Why don't you check that answer? – Taufiq Rahman Nov 19 '16 at 13:59
  • I've taken a look but I couldn't see how to keep the other part lowercase. Please do feel free to post an answer so I can close this @Achilles – Jay Rosenthal Nov 19 '16 at 13:59
  • So you want the print statement to print the teaname in uppercase? and then "selected!" ? Please show the output you want on your post. – Taufiq Rahman Nov 19 '16 at 14:12
  • Tea name as a title, and selected in lower case. Question edited @Achilles – Jay Rosenthal Nov 19 '16 at 14:15
  • @JayRosenthal understood.You can try my code. – Taufiq Rahman Nov 19 '16 at 14:20
  • Much appreciated. Will help in the future @Achilles – Jay Rosenthal Nov 19 '16 at 14:24
  • This is not a duplicate - I am asking how to use titles in only some areas of print and exclude the rest @TigerhawkT3 – Jay Rosenthal Nov 19 '16 at 14:32
  • @JayRosenthal - Yes it is a duplicate. You already have the "some areas of print" that you intend to convert to titlecase saved as its own variable. Titlecase that variable. If your question is about how to insert variables into a string, that would be [a different duplicate](http://stackoverflow.com/questions/2960772/putting-a-variable-inside-a-string-python). – TigerhawkT3 Nov 19 '16 at 15:18

2 Answers2

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

out:

What tea would you like? English Breakfast, Green Tea or Earl Grey? earl grey
Earl Grey selected!
宏杰李
  • 11,820
  • 2
  • 28
  • 35
0

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.")
Taufiq Rahman
  • 5,600
  • 2
  • 36
  • 44