Flip a graph

Jason Friedman jsf80238 at gmail.com
Sat Jan 4 11:15:39 EST 2014


I am teaching Python to a class of six-graders as part of an after-school
enrichment.  These are average students.  We wrote a non-GUI "rocket
lander" program:  you have a rocket some distance above the ground, a
limited amount of fuel and a limited burn rate, and the goal is to have the
rocket touch the ground below some threshold velocity.

I thought it would be neat, after a game completes, to print a graph
showing the descent.

Given these measurements:
measurement_dict = { # time, height
    0: 10,
    1: 9,
    2: 9,
    3: 8,
    4: 8,
    5: 7,
    6: 6,
    7: 4,
    8: 5,
    9: 3,
    10: 2,
    11: 1,
    12: 0,
}

The easiest solution is to have the Y axis be time and the X axis distance
from the ground, and the code would be:

for t, y in measurement_dict.items():
    print("X" * y)

That output is not especially intuitive, though.  A better visual would be
an X axis of time and Y axis of distance:

max_height = max(measurement_dict.values())
max_time = max(measurement_dict.keys())
for height in range(max_height, 0, -1):
    row = list(" " * max_time)
    for t, y in measurement_dict.items():
        if y >= height:
            row[t] = 'X'
    print("".join(row))

My concern is whether the average 11-year-old will be able to follow such
logic.  Is there a better approach?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20140104/f5405e95/attachment.html>


More information about the Python-list mailing list