Want to keep learning?

This content is taken from the Raspberry Pi Foundation & National Centre for Computing Education's online course, Representing Data with Images and Sound: Bringing Data to Life. Join the course to learn more.

ASCII art

This coding task includes some of the concepts that you should already be familiar with in Python in order to get the most out of this course. If you’re unsure, all these topics and more are covered in our courses Programming 101: An Introduction to Python for Educators and Programming 102: Think Like a Computer Scientist.

The aim of this program is to output a smiley face that is represented by characters, known as ASCII art. A 2D list is used to store the rows and the columns of the smiley face. Each character should be printed individually.

The code contains comments but some of the code is missing or incomplete. Read through the program and fix the code so that it works as intended. The missing code is listed below after the incomplete program.

Incomplete program

# define a 2D list of characters that represents a smiley face
smiley = [[" "," "," "," "," "," "," "," "," "," "," "," "],
          [" "," "," ","*"," "," "," "," ","*"," "," "," "],
          [" "," ","*","*","*"," "," ","*","*","*"," "," "],
          [" "," ","*","*","*"," "," ","*","*","*"," "," "],
          [" "," "," ","*"," "," "," "," ","*"," "," "," "],
          [" "," "," "," "," "," "," "," "," "," "," "," "],
          ["*","*"," "," "," "," "," "," "," "," ","*","*"],
          [" ","*","*"," "," "," "," "," "," ","*","*"," "],
          [" "," ","*","*","*","*","*","*","*","*"," "," "],
          [" "," "," ","*","*","*","*","*","*"," "," "," "]]

# find the total number of rows
total_rows = len(smiley)

# repeat the outer loop based on the total number of rows
for row in range( ):

    # find the number of values in the current row
    total_cols = len(smiley[row])

    # repeat the inner loop based on the total number of cols
    
        
        # output the value from the current row and column
        # setting end to an empty string stops a new line from being created
        print(smiley[ ][ ], end="  ")
        
    # output a new line after each row has ended
    print("\n")

Missing code

row

col

total_rows

for col in range(total_cols):

How did you get on?

These programming concepts are going to be used and expanded upon throughout the remainder of the course. If you found some of these concepts challenging and think you need a Python refresher first, try one of our other online courses. In our course Programming 101: An Introduction to Python for Educators we cover built-in functions such as print(), creating and using variables, and repeating sections of code with while and for loops. Our course Programming 102: Think Like a Computer Scientist introduces you to data structures, such as lists which are used to store and organise data.

Share this article:

This article is from the free online course:

Representing Data with Images and Sound: Bringing Data to Life

Raspberry Pi Foundation