0

Working through a coding book with my son, "Help Your Kids with Computer Coding" by DK. Great book so far.

For a project we're building a drawing machine, that's supposed to split a string into a series of commands that will be drawn.

But the code skips to the final item in the string without outputting the other items. The book says this command, loops through the list of items in the string:

for cmd in cmd_list:

Is there something missing to make it go through every element of the string? Below is a sample of the code and then the output.

Thanks!

#String Artist
def string_artist(program):
    cmd_list = program.split('-')
    print(cmd_list) #I added this line to confirm the split#
    for cmd in cmd_list:
        cmd_len = len(cmd)
        if cmd_len == 0:
            continue
    cmd_type = cmd[0]
    num = 0
    if cmd_len > 1:
        num_string = cmd[1:]
        num = int(num_string)
    print(cmd, ':', cmd_type, num)
    turtle_controller('cmd_type', num)

outputs:

>>> string_artist('N-L90-F100-F101-F102')
['N', 'L90', 'F100', 'F101', 'F102']
F102 : F 102
Unrecognized Command

Any help will be greatly appreciated! Want to keep moving forward with my son's learning this. Thanks!

1 Answer 1

1

The issue has to do with indentation. Here is your original function, once again:

def string_artist(program):
    cmd_list = program.split('-')
    print(cmd_list) #I added this line to confirm the split#
    for cmd in cmd_list:
        cmd_len = len(cmd)
        if cmd_len == 0:
            continue
    cmd_type = cmd[0]
    num = 0
    if cmd_len > 1:
        num_string = cmd[1:]
        num = int(num_string)
    print(cmd, ':', cmd_type, num)
    turtle_controller('cmd_type', num)

Note the line that says cmd_type = cmd[0] is not inside the for-loop. In fact, that line and all other lines after it are not in the for-loop, either. The way it's written now, they will be executed only after the for-loop ends. This is technically a valid thing to do, because the variable cmd still exists after the loop is done (it's value will be whatever value it happened to take on in the loop's last iteration.)

The solution involves indenting the lines of code I mentioned, so that they are in the for-loop:

def string_artist(program):
    cmd_list = program.split('-')
    print(cmd_list) #I added this line to confirm the split#
    for cmd in cmd_list:
        cmd_len = len(cmd)
        if cmd_len == 0:
            continue
        cmd_type = cmd[0]
        num = 0
        if cmd_len > 1:
            num_string = cmd[1:]
            num = int(num_string)
        print(cmd, ':', cmd_type, num)
        turtle_controller('cmd_type', num)
Sign up to request clarification or add additional context in comments.

1 Comment

You are correct! I figured it out after starting from scratch and noticed the indents. Appreciate you're help! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.