Mind boggler


#1

Hey guys!
I'm doing a separate project...This is my code...

new = []
def extract_nums(string):
    trash = []
    for x in string:
        if x.isalpha:
            trash.append(x)
        else:
            new.append(x)
print new
extract_nums("hello 123")

Apperently, it only prints two brakets, []


#2

@drewteriyaki,
Try it with Duck typing

def extract_nums(string):
    new = []
    trash = []
    for x in string:
        try:
            x = int(x)
            trash.append(x)
        except Exception:
            new.append(x)
    print("isalpha: {0}".format(new))
    print("trash  : {0}".format(trash))

extract_nums("hello 123")

#3

Hey Drewteriyaki,

Swap the order around so you're printing new after running extract_nums - new is empty by default, and so has to be modified before being printed if you want to have anything in it when you print the list :slight_smile: