-4
def gender_checker(sex="unknown"):
    if sex=="m":
        print ("male"):
    elif sex=="f":
        print ("female")
    elif sex is not "m" or "f":
        print (" invalid")

""" It's python program..(3.x.xx ) Problem is when no arguments is being passed it should print "unknown" buts its not printed that instead "invalid" is getting printed. What I want is if no args are passed then print default if args passed is in valid then print invalid that its """

U13-Forward
  • 69,221
  • 14
  • 89
  • 114

3 Answers3

0

Python isn't fully like English writing, so you would have to do:

def gender_checker(sex="unknown"):
    if sex=="m":
        print ("male"):
    elif sex=="f":
        print ("female")
    else:
        print ("unknown")
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • Hey thanks What I want is when user passes no arguments then it should print "unknow" if user enter wrong arguments kike "a" which is invalid so it should print invalid , now suggest me how to modify my code to do this? – user9562444 Feb 05 '19 at 17:29
0

I think you are misunderstanding your if-else statement. It will print invalid since your last elif statement is checking if it is not m or f; I would imagine you want something like this:

def gender_checker(sex="unknown"):
    if sex=="m":
        print("male"):
    elif sex=="f":
        print("female")
    elif sex == "unknown":
        print("unknown")
    else:
        print("invalid")
fixatd
  • 1,394
  • 1
  • 11
  • 19
  • Hey thanks What I want is when user passes no arguments then it should print "unknow" if user enter wrong arguments kike "a" which is invalid so it should print invalid , now suggest me how to modify my code to do this? – user9562444 Feb 05 '19 at 17:28
  • @user9562444 then you would want to add an `elif` statement for `unknown`. I've updated my answer just as how you described it – fixatd Feb 06 '19 at 06:37
  • Hey thanks this is what i wanted :), Can you point out where i was slipped? i might have misunderstood some concept please clarify this thanks again – user9562444 Feb 06 '19 at 08:00
  • @user9562444 just on your last `elif` statement, as you've mentioned you only needed to check if the user had not provided a gender and by default sex will be set to "unknown" if you don't provide any arguments to `gender_checker`. As you can see in the answer I've added an additional `elif` for sex == "unknown" which covers your default `sex`. The `else` is to cover all other `sex` inputs that are not covered by `m`, `f`, `unknown`. You can mark my post as correct if it answers your question. Thanks! – fixatd Feb 06 '19 at 11:51
  • thanks for clarification :) – user9562444 Feb 09 '19 at 04:36
-1

The last condition should be:

elif sex != "m" or sex != "f":

But even simpler:

else:

Because this will cover all other cases where sex is neither "m" nor "f".

In addition remove the : behind print("male"): and let it print after the last (else:) clause: print("unknown")

Gwang-Jin Kim
  • 9,303
  • 17
  • 30