Case insensitivity in "if" statement (Python) -
this question has answer here:
i working on project @ 1 point ask user yes/no question. use code handle such questions:
def yn(): global finalchoice choice=str(raw_input("y/n: ")) if choice == "y": finalchoice="true" elif choice == "y": finalchoice="true" elif choice == "n": finalchoice="false" elif choice == "n": finalchoice="false" else: yn() pass
but seems quite inefficient, have check both "y" , "y" or "n" , "n" separately. i've tried:
if choice == "y" or "y": finalchoice="true"
unfortunately, ignore 'else' command , pass whatever give it.
any tips?
if choice == "y" or "y":
not right! evaluate true
.
it (choice == "y")
or ("y")
. , latter true
because non empty string in python logically evaluates boolean true.
you should doing:
if choice in ["y","y"]:
or,
if choice == "y" or choice == "y":
or can use:
if choice.lower() == "y": finalchoice="true"
Comments
Post a Comment