2

If the user has inputted 'tea' or 't' as their choice, how do I define the variable beverage.lower() as 'tea'? Because of the

if beverage.lower()=='t'or beverage.lower()=='tea':
                beverage.lower()=='tea'

line has absolutely no whatsoever effect on the overall program itself?

while True:
        beverage=raw_input("What is your preferred beverage: coffee, or tea?")
        if beverage.lower() not in ('coffee','tea','t','c'):
            if beverage.lower()=='t'or beverage.lower()=='tea':
                beverage.lower()=='tea'
            elif beverage.lower()=='c'or beverage.lower()=='coffee':
                beverage.lower()=='coffee'
            print("Sorry! I didn't quite catch that. Please try again! (Note that you can use the letter 'c' or the letter 't' to denote coffee or tea respectively!)")
            continue
        else:
            print("Ah! Fantastic choice!")
            break

Likewise, how would I go about defining the user inputted beverage.lower() as another variable?

5
  • 2
    the expression beverage.lower() means you call the method lower() of the instance of the string class. So you can do what you want. Commented Oct 3, 2018 at 6:57
  • Do not mix 4 tabs and 8 tabs indentation. Commented Oct 3, 2018 at 7:11
  • 2
    Why not just lower it once? beverage = beverage.lower() Commented Oct 3, 2018 at 7:15
  • 1
    @Vineeth Sai: That's referred to as tabs of 4 spaces and tabs of 8 spaces—but you're right that they should not be mixed together in the same code. Commented Oct 3, 2018 at 8:07
  • @martineau Thank you for the correction, Updated it. Commented Oct 3, 2018 at 8:42

5 Answers 5

5

Your code had various issues in flow control(if blocks) and value assignments( == and =). After some changes, here it is. And try not to mix tabs of 4 spaces and tabs of 8 spaces. Always stick to 4 space tab as it is standard from PEP8

while True:
    beverage = input("What is your preferred beverage: coffee, or tea?").lower() # much efficient to .lower() only once
    if beverage in ('coffee','tea','t','c'): # Change this to 'in'
        if beverage == 't' or beverage == 'tea':
            beverage = 'tea' # value assignments are done with = not ==
        elif beverage == 'c' or beverage == 'coffee':
            beverage = 'coffee' # value assignments are done with = not ==
        print('Ah! Fantastic choice!')
        print('You have chosen {}'.format(beverage))
    else:
        print("Sorry! I didn't quite catch that. Please try again! (Note that you can use the letter 'c' or the letter 't' to denote coffee or tea respectively!)")

O/P:

What is your preferred beverage: coffee, or tea?c
Ah! Fantastic choice!
You have chosen coffee
What is your preferred beverage: coffee, or tea?t
Ah! Fantastic choice!
You have chosen tea
What is your preferred beverage: coffee, or tea?x
Sorry! I didn't quite catch that. Please try again! (Note that you can use the letter 'c' or the letter 't' to denote coffee or tea respectively!)
Sign up to request clarification or add additional context in comments.

1 Comment

I see my problems now. I appreciate it a lot. Thank you!
3

just use the variable to assign value:

if beverage.lower()=='t'or beverage.lower()=='tea':
    beverage = 'tea'

assigning another variable:

input_var = None
beverage=raw_input("What is your preferred beverage: coffee, or tea?")
if beverage.lower()=='t'or beverage.lower()=='tea':
    input_var = 'tea'

print 'Input value: ', input_var

Comments

0

You can use the statement beverage=beverage.lower() if you want to modify the value of the variable beverage.

From your question I believe that was your doubt.

Comments

0

if beverage.lower()=='t'or beverage.lower()=='tea': beverage.lower()=='tea'

Above statement has no effect as you said, because you have written it so. If input is not in ('coffee','tea','t','c'), there is absolutely no chance it will go under above code.

You need to replace if beverage.lower() not in ('coffee','tea','t','c'): with

if beverage.lower() in ('coffee','tea','t','c'):

Also you need to replace beverage.lower()=='tea' with beverage.lower()='tea' inside the if condition

Comments

0

You are making a small mistake while assigning the value 'tea' or 't'. It should be like - beverage='tea' instead of beverage.lower()=='tea'

3 Comments

What does beverage.lower() = 'tea' do?
My mistake it should be beverge='tea'.
If you realise your mistake you can improve your answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.