0
def check(str):
    if(str.isalnum())==True:
        return True
    if(str.isalpha())==True:
        return True
    if(str.isdigit())==True:
        return True
    if(str.islower())==True:
        return True
    if(str.isupper())==True:
        return True


if __name__ == '__main__':
    s = input()
    if(check(s)):
        print('True')
    else:
        print('False')

It is showing only one condition's result. For example, if I type qA2 it is showing one True instead of

True
True
True
True
True
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Stark_Tony
  • 77
  • 3
  • 9

3 Answers3

1

This is because you are "returning" from the function if one of the if conditions evaluates to true.

You can read more about the keyword return here. The gist is, as the word says, it "returns" from the function with specified value(s).

If you want the other if conditions to get evaluated too, you will need to maintain a small data structure of all successful if conditions, eg:

def check(str):
    # let's initialise a dictionary with all False values.
    # If any if condition turns True, then we will set 
    # that value to True in the dictionary.
    res = {
        "isalnum":False,
        "isalpha":False,
        "isdigit":False,
        "islower":False,
        "isupper":False,
    }

    if(str.isalnum())==True:
        res["isalnum"] = True
    if(str.isalpha())==True:
        res["isalpha"] = True
    if(str.isdigit())==True:
        res["isdigit"] = True
    if(str.islower())==True:
        res["islower"] = True
    if(str.isupper())==True:
        res["isupper"] = True

    return res # This returns a dictionary with all values
if __name__ == '__main__':
    s = input()
    if(check(s)):
        print('True')
    else:
        print('False')
Rahul Bharadwaj
  • 2,555
  • 2
  • 18
  • 29
0

return leaves the function immediately. The first condition which succeeds will cause the rest to be skipped, the way you defined your function.

If you want to check all the conditions every time, try something like

def check(str):
    whether = True
    if not str.isalnum():
        whether = False
    if not str.isalpha():
        whether = False
    if not str.isdigit():
        whether = False
    if not str.islower():
        whether = False
    if not str.isupper():
        whether = False
    return whether

Printing something in each branch seems excessive, but could certainly be added.

tripleee
  • 175,061
  • 34
  • 275
  • 318
0

return stops the function's execution. Nothing after a return statement will be run, and a function can only have a single return value. If you want to see the result for all conditions, store them in a List or a Dict and return that instead, for example:

def check(str):
    results = {}
    results['isalnum'] = str.isalnum()
    results['isalpha'] = str.isalpha()
    results['isdigit'] = str.isdigit()
    results['islower'] = str.islower()
    results['isupper'] = str.isupper()
    return results
ooa
  • 463
  • 2
  • 8