1
def checker(a_list):
    for item in a_list:
        if str(item).isdigit():
            return True
        else:
            return False

The variable I have for checker is a list of four string containing variables. I planned to use it as a 'checker' to see if all input values of another function contained only digits.

Problem: If the first item in a_list is not a number, checker returns False as it should. However, if the first item is a number while any others in the list aren't, checker returns True anyways. This causes the next function to proceed with the non-number variables and results in an error.

How do I make it so my function checks the entire list before returning True? Or if need be, how do I make a new function that does what I'm looking for? Thanks

wwii
  • 23,232
  • 7
  • 37
  • 77
Paul T
  • 13
  • 3
  • do you want to check all the members of list as digit ? – babygame0ver Dec 31 '17 at 07:29
  • The `return` statement exits the whole function (see https://www.python-course.eu/python3_functions.php) so the first iteration of the `for` loop will hit a `return` and end the function. I suggest you do the iteration and only return `False` if not digit within the loop and a final `return True` after the `for` loop. You can also take a look to `all` https://docs.python.org/3/library/functions.html#all – Savir Dec 31 '17 at 07:32
  • 1
    Don't `return True` *in* the loop. In the loop check if the item `not .isdigit`. Move `return True` after the loop has completed. – wwii Dec 31 '17 at 07:32
  • Please post a minimal example of an input (`a_list`) that should *evaluate* to `True` and one that *evaluates* to `False`. ... [mcve] – wwii Dec 31 '17 at 17:24

4 Answers4

4

There are usefull built-in fuctions all (and any) for checking multiple conditions:

def checker(a_list):
    return all(str(item).isdigit() for item in a_list)
VPfB
  • 14,927
  • 6
  • 41
  • 75
  • yep, answer of @wwii better for speed reasons, your answer more pythonic. So `any(not str(item).isdigit() for item in a_list)` will be ideal answer :) – El Ruso Dec 31 '17 at 08:04
  • 1
    @ElRuso `all(x ...)` and `not any(not x ...)` are equivalent. https://en.wikipedia.org/wiki/De_Morgan%27s_laws – VPfB Dec 31 '17 at 08:43
  • you right, it will be [short cutted](https://stackoverflow.com/a/39711683/4249707) quite the same – El Ruso Dec 31 '17 at 15:31
  • @ElRuso - what is less pythonic about a for loop? – wwii Dec 31 '17 at 16:05
  • Alternate - `all(map(str.isdigit, map(str, a_list)))` – wwii Dec 31 '17 at 18:24
  • @wwii IMHO use built-in functions is more pythonic than writing exactly the same again – El Ruso Jan 01 '18 at 18:01
4

Don't return True in the loop. In the loop return False if an item is NOT a digit. Move return True after the loop has completed.

def checker(a_list):
    for item in a_list:
        if not str(item).isdigit():
            return False
    return True
wwii
  • 23,232
  • 7
  • 37
  • 77
  • This answer is derived from the original code, that's why +1 for being educative. – VPfB Dec 31 '17 at 15:29
  • @PaulToth - pretty sure this solution is functionally equivalent to VPIB's solution. Could you provide an example input that this function produces an incorrect result? – wwii Dec 31 '17 at 17:35
  • @PaulToth - should the function return True or False for `[12, 15, 54, 72]`? – wwii Dec 31 '17 at 17:46
  • @wwii My mistake. I incorrectly read your suggestion and was being faced by the exact same problem as before. upon more careful implementation it this fix is working just as well. Thank you so much for the help! Also the list you posted should and does return True. – Paul T Dec 31 '17 at 17:52
1

I'm assuming you want to check that all elements of a_list return True as a return value from isdigit().

In this case, use the built-in function all

all(str(s).isdigit() for s in a_list)

for more information on any and all, check out this link on SO: any and all explained

edit: thanks @RoadRunner for pointing out conversion to str as OP had it in it's example he gave.

MSKW
  • 55
  • 3
  • 7
0

This should check if all items in the list are digits

if all(str(x).isdigit() for x in a_list):
    return True
else:
    return False
Dalvenjia
  • 1,953
  • 1
  • 12
  • 16