0
def question2_a(num_str):  # 1001
    if len(num_str) == 0:
        return True
    if num_str[0] != ("0" or "1"):
        return False
    return question2_a(num_str[1: ])

print(question2_a("100"))

I cant understand, why do I get False?? the numbers are "0" or "1", but I still get False...

same with this code:

def question2_a(num_str):  # 1001
    if len(num_str) == 0:
         return True
    if num_str[0] == ("1" or "0"):
         return question2_a(num_str[1: ])

 print(question2_a("1001010"))

Why do I get None here? it is really annoying, I am doing nothing wrong, but I keep getting None here although there is no problem

And lastly:

def question2_a(num_str):  # 1001
    if len(num_str) == 0:
        return True
     if num_str[0] == ("1" or "0"):
        return question2_a(num_str[1: ])
    else:
         return False

 print(question2_a("1001010"))

Why do I get False here also? I should get True..

  • 1
    Python is not English. `("0" or "1")` returns `"0"`. I'd say `num_str[0] in {"0", "1"}` – Peter Wood May 20 '22 at 09:55
  • @PeterWood What do you mean? I am doing indexing, if the index is "0" or "1", then just call the function again... – TryingToMath May 20 '22 at 09:55
  • You're not reading what @PeterWood is saying: `'0' or '1'` evaluates to `'0'` because the logical operator finds `'0'` 'truthy' and the resulting value of the expression is `'0'`. This is a commonly asked question on StackOverflow (sometimes about 'or', sometimes about 'and') - the bottomline is "that's not how and/or works", you cannot `or` two values together and expect Python to understand what you meant (in English) – Grismar May 20 '22 at 09:58
  • @Grismar Then how am I supposed to call "0" or "1" if I cannot do it?... its impossible to call it then lol... – TryingToMath May 20 '22 at 10:03
  • 1
    @TryingToMath I showed you how – Peter Wood May 20 '22 at 10:07
  • @PeterWood yea but we are not allowed to use in, that is the problem. – TryingToMath May 20 '22 at 10:11
  • `num_str[0] == "0" or num_str[0] == "1":` – Peter Wood May 20 '22 at 10:14
  • @PeterWood I tried it before too, but it did not work. It gave me the same problem. – TryingToMath May 20 '22 at 10:15
  • 1
    See how to create a [mcve]. What I've posted should work. If it doesn't work, you're doing something wrong. Try printing the value of `num_str[0]` to validate your understanding. – Peter Wood May 20 '22 at 10:25
  • @PeterWood Thanks, I will try it in a bit and update if I managed to do it :) – TryingToMath May 20 '22 at 10:26
  • @PeterWood https://pastebin.com/UiKgPA9S - See , it is still happening... I did as you said – TryingToMath May 20 '22 at 10:30
  • https://pastebin.com/RCDpbaQ6 Okay, I think I managed to make it work, but it still doesnt help me.. why did it not work when I did != and it worked when I did ==? @PeterWood – TryingToMath May 20 '22 at 10:42
  • 1
    @TryingToMath think about it. If the value is `"0"` then one of the expressions will be `True`. Same for `"1"`. Probably what you want is `if (num_str[0] != "0") and (num_str[0] != "1"):` – Peter Wood May 20 '22 at 11:50
  • 1
    @PeterWood ohhh right, I see what you say.. didnt notice.. Thanks. – TryingToMath May 20 '22 at 12:31
  • I love those "ohhh right" moments. All the best. – Peter Wood May 20 '22 at 14:39

0 Answers0