Fluxcapacitor
Fluxcapacitor

Reputation: 393

"or" condition causing problems with "if"

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

Answers (4)

kojiro
kojiro

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

user234932
user234932

Reputation:

It would be easier to just use the in operator in this case:

if choice in ['A', 'a']: ...

Upvotes: 2

Michael Davis
Michael Davis

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

Keith Nicholas
Keith Nicholas

Reputation: 44316

if choice == 'A' or choice =='a':

Upvotes: 0

Related Questions