python - Looking for other possible ways to check if a variable means something -
i've been trying make program calculates age of someone told me doing wrong. had
if monthinput == "january" or "1": monthinput = 1 validmonth = true but apparently make validmonth true if "bob" inputted. told replacing first line with
if monthinput in ["january", "1"]: would trick. do
if monthinput == "january" or monthinput == "1": instead of that? i'm trying see options (i'm still learning helpful future).
yes, can monthinput == "january" or monthinput == "1". boolean logic point of view, same monthinput in ["january", "1"].
there differences between these statements, they'll both produce either true or false, , both return same output exact same values of mothinput.
you can try out statements these in python interactive interpreter:
>>> monthinput = "1" >>> monthinput == "january" or monthinput == "1" true >>> monthinput in ["january", "1"] true etc.
if want dig down this, best method use set:
monthinput in {'january', '1'} because set membership testing takes constant cost (independent of size of set) chaining or statements or using list test against takes linear cost (the more elements need test against longer takes).
Comments
Post a Comment