I have an small issue with iteration

def simple_assembler(program):
    for instruction in program:
        for element in instruction:
            if element.isdigit() == True:
                return element

simple_assembler(['mov a 5','inc a','dec a','dec a','jnz a -1','inc a'])

This code works but once it finds the first case that is true and returns the element, the program terminates. It should iterate through the entire program, which is a list of strings. Thanks

You’ll have to do something with elements that are digits besides returning them. return always exits a fuction. Maybe add the elements to a new list, and return the list after iterating through all instructions?

1 Like

That’s a brilliant idea

1 Like

.isdigit is a predicate, and therefore already boolean. We do not ever need to compare a boolean.

if element.isdigit():

Now the question becomes where do you expect to find a digit?

>>> for x in 'mov a 5':
	print (x)

	
m
o
v
 
a
 
5
>>> 
>>> for x in 'mov a 5'.split():
	print (x)

	
mov
a
5
>>> 
1 Like