Back

Draw a histogram

Introductory tasks
This is a nice task using for loops and a list of data. It might be worth reminding students about how a for loop works in Python and how nested for loops work. 

The problem
Students have to write a program that accepts a list of data and produces a histogram. E.g. if the data is [5, 8, 10, 3] the program should print out:

* * * * *
* * * * * * * * 
* * * * * * * * * *
* * * *
 

Overview of our solution

function for drawing the histogram
function for main()

main() 

Pseudo-code

def histo(myData):
    for each piece of data in the myData:
        for each number:
            print * and stay on same line
        new line

def main():
    myData = [5, 8, 10, 3]
    histo (myData)

Solution

histo

Extension
Students could print out the data at the start of each line of asterisks. They could give the user the option of what symbol to use to print the histogram. They could allow the user to enter in the data at runtime.  

Back