CIS 130 - Intro to Programming
p. 3
Week 10 Lab Exercise - 03-28-07
Write a function bar_chart that expects a list of numbers as its argument; it should use appropriately use
line_of_X to print a line of X's as long as each value in that list. For example, bar_chart( [1, 8, 5, 3] )
would cause the following to be printed to the screen:
X
X X X X X X X X
X X X X X
X X X
When it is your turn, you will show me the results of the following call: bar_chart( [1, 8, 5, 3] )
17. Now do you see how you can combine read_nums and bar_chart to print to the screen a bar chart of all of
the numbers from a given file?
Write a function file_chart that expects as its argument the name of a file in the current directory containing
one value per line, that then prints to the screen a bar chart corresponding to the values in that file. The body
of file_chart should use read_nums and bar_chart, and if its body is longer than 2-3 lines then you are
over-complicating it...
When it is your turn, you will show me the results of the following call: file_chart("test.txt")
Its results should look like:
X X X X X
X X X X X X X X
X X
X X X X X X X
X
X X X X
18. Finally, you have a client who is terrified of pico, but willing to use the Python interpreter. (Go figure!)
He/She wants a Python function create_file that will accept the name of a file as its argument, and then
prompt him/her for lines of input, which should then be written to that file (existing file contents from before
this call can be cheerfully deleted). It should keep prompting until he/she enters the line "stop".
When it is your turn, I will watch you run create_file("try.txt"), type in a few lines and then stop when
prompted, and then look at the contents of "try.txt".
NOW write your name(s) on the NEXT: list on the board. (You write your name on this list if you have
questions along the way, as well as when you are done; I'll then work my way down the list.) You need to
complete the above and have it checked by the end of lab.