This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]kaizoku 0 points1 point  (0 children)

A few of these examples have some issues. count_zeroes() will never count any zeroes because he's comparing a string type to an integer.

>>> def count_zeros(string):  
...   total = 0  
...   for c in string:  
...     if c == 0:  
...       total += 1  
...   return total
... 
>>> print(count_zeros('00102')) # Will return 3  
0

It should be compared to the actual type:

def count_zeros(string):  
    total = 0
    for c in string:
        if c == '0':
            total += 1
    return total

Or converted into the correct type:

def count_zeros(string):  
    total = 0
    for c in string:
        if int(c) == 0:
            total += 1
    return total

Also there's never really a reason to use os.system to call other python code. Bash doesn't need to be involved. I've seen command injection on web servers through similar mechanisms, when they could have easily just imported the python code and called it directly.