0

I am working on this CodeWars Kata and just to get started I decided to split the given numbers into individual list items to check and see if the amount of digits was 2 or greater but when I run this code block it returns false even though when I print the resultant list out it has more than one item in the list. I believe the function should be returning my print statement of "numbers not asc".

Am I missing something super obvious?

def sel_number(n, d):
#converts digits into individual ints and then converts those into strings and loads into a list
    first_num = [int(a) for a in str(n)]
    second_num = [int(b) for b in str(d)]
    #check if length of list is 2 or more otherwise return false
    if len(first_num) or len(second_num) <= 1:
        return False
    #check if numbers in list are ascending
    elif first_num != sorted(first_num) or second_num != sorted(second_num):
        print('numbers not asc')
    else: 
        return True

sel_number(21, 21)
  • 1
    You might have wanted `len(first_num) <= 1 or len(second_num) <= 1` instead. – j1-lee Aug 18 '22 at 17:21
  • `if len(first_num) or len(second_num) <= 1` is the problem. This means `if (len(first_num)) or (len(second_num) <=1)`. The first part of that `if` statement is true, because `len(first_num)` is 2, and a non-zero number is treated as True (it's "truthy") for these purposes in Python. – slothrop Aug 18 '22 at 17:21
  • Aha! So it was something super obvious but I also learned something new so, thank you!! – Noah Toomey Aug 18 '22 at 17:28

0 Answers0