Reputation: 41
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?
Upvotes: 2
Views: 209
Reputation: 107
You are making a small mistake while assigning the value 'tea' or 't'. It should be like - beverage='tea' instead of beverage.lower()=='tea'
Upvotes: 0
Reputation: 3447
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!)
Upvotes: 5
Reputation: 221
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
Upvotes: 0
Reputation: 325
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
Upvotes: 3
Reputation: 100
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.
Upvotes: 0