Reputation: 393
I'm having trouble with an or
condition in a function. The if
statement keeps evaluating as True
no matter what value choice
is. When I remove the or
, the if
works correctly.
def chooseDim ():
**choice = input ('Do you need to find radius or area? ')
if choice == 'A' or 'a':**
area = 0
area = int(area)
areaSol ()
elif choice == 'R' or 'r':
radSol ()
else:
print ('Please enter either A/a or R/r.')
chooseDim ()
Upvotes: 3
Views: 142
Reputation: 77167
The answers about or
itself are correct. You're literally asking if "a"
is True, which it always is. But there's an alternative approach:
if choice in 'Aa':
Then again, there's nothing wrong with:
if choice.lower() == 'a':
Upvotes: 4
Reputation:
It would be easier to just use the in
operator in this case:
if choice in ['A', 'a']: ...
Upvotes: 2
Reputation: 2430
'a' evaluates to True, so you need to construct your if statement correctly.
def chooseDim ( ):
**choice = input ('Do you need to find radius or area? ')
if choice == 'A' or choice == 'a':**
area = 0
area = int(area)
areaSol ( )
elif choice == 'R' or choice == 'r':
radSol ( )
else:
print ('Please enter either A/a or R/r.')
chooseDim ( )
Upvotes: 4