Flip a graph

J

Jason Friedman

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?
 
W

Wiktor

My concern is whether the average 11-year-old will be able to follow such
logic. Is there a better approach?

Basically mine approach is the same, but maybe is easier to explain it to
kids.


max_height = max(measurement_dict.values())

temporary_graph = []

for t, y in measurement_dict.items():
temporary_graph.append('X'*y + ' '*(max_height - y))

for i in range(max_height-1, -1, -1):
for item in temporary_graph:
print(item, end='')
print()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top