-1

The problem is complete the solution, a function that verifies that the string s is 4 or 6 long and consists only of numbers. For example, if s is "a234", return False, and if s is "1234", return True.

I didn't know why return False when s is "1234" in line 2 to line 3??

How can i fix it?

The len(s) is 4!

if len(s) != 4 or len(s) !=6:
        return False
  • 1
    your code returns false if either len is different than 4 or len is different than 6. Think about it, it triggers always for any length. –  Jul 22 '22 at 13:41
  • 4
    ***Everything*** is '!= 4 or != 6', including 4 and 6 and 12837198273. – luk2302 Jul 22 '22 at 13:42
  • [Why does non-equality check of one variable against many values always return true?](https://stackoverflow.com/q/26337003/3890632) – khelwood Jul 22 '22 at 13:44
  • The length is 4, but the length is not 6. You want `and`, not `or`. – chepner Jul 22 '22 at 13:51

1 Answers1

2

You can use:

len(s) in (4,6) and s.isdigit()

example:

  • s = '1234': True
  • s = 'a234': False
  • s = '12345': False

Your attempt failed because len(s) != 4 or len(s) !=6 is always True (to be False you would need a number that is both 4 and 6 at the same time: impossible)

mozway
  • 194,879
  • 13
  • 39
  • 75