Keep getting error when argument is [1, 2, 2]
Question:
def is_ascending(items):
if len(items) == 0 or 1:
return True
for i in range(len(items) - 1):
if items[i] >= items[i + 1]:
return False
return True
Answers:
if len(items) == 0 or 1:
This snippet doesn’t do what you think it does. It’s essentially evaluated like
if (len(items) == 0) or (1):
which is always True since the part after the "or" is True (so the whole function returns True. What you want is
if (len(items) == 0) or (len(items) == 1):
or simpler
if len(items) <= 1:
def is_ascending(items):
if len(items) == 0 or 1:
return True
for i in range(len(items) - 1):
if items[i] >= items[i + 1]:
return False
return True
if len(items) == 0 or 1:
This snippet doesn’t do what you think it does. It’s essentially evaluated like
if (len(items) == 0) or (1):
which is always True since the part after the "or" is True (so the whole function returns True. What you want is
if (len(items) == 0) or (len(items) == 1):
or simpler
if len(items) <= 1: