Skip main navigation

ASCII art

Create an ASCII art smiley using Python and 2D lists.

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.

This article is from the free online

Data Representation in Computing: Bring Data to Life

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education