List Applications
# Creating a histogram from a list of values. values = [] # a list of values # input 10 values from user print ("Enter 10 integers:") for i in range( 10 ): newValue = int( input( "Enter integer %d: " % ( i + 1 ) ) ) values += [ newValue ] # create histogram print ("\nCreating a histogram from values:") print ("%s %10s %10s" % ( "Element", "Value", "Histogram" )) for i in range( len( values ) ): print ("%7d %10d %s" % ( i+1, values[ i ], "*" * values[ i ] ))
ClassworkHomework:
Exercise 1: NoDuplicates_YI.py
Use a list to solve the following problem: Read in 20 numbers. As each number is read, print it only if it is not a duplicate of a number already read.
Make it an interactive program:
1. A message should be displayed when the user enters a number that has already entered as input.
2. As a way to validate no repeats it prints back the number.
Sample Output:
Enter a number: 2
2
Enter a number: 4
4
Enter a number: 5
5
Enter a number: 2
2 is a duplicate
Enter a number: 10
10
…
Exercise 2: StudentPoll_YI.py
Write a python program StudentPoll_YI.py that uses a list to summarize data collected in a survey. Consider the following problem statement:
Sixteen students were asked to rate on a scale of 1 to 10 the quality of the food in the student cafeteria, with 1 being “awful” and 10 being “excellent”. Place the 16 responses in a list and determine the frequency of each rating.
Randomly assign 16 numbers between 1 and 10 to a list, survey, and use this list as an index of another list, ranking, to keep track of the count of the responses.
Create a histogram with the ranking data.
A little help…
ranking = [0,0,0,0,0,0,0,0,0,0,0] survey = [1,5,3,4,1,1,1,2,1,2,1,3,4,5,1,7] Do not use "if's" statements. Instead use the following: ranking[ survey[ i ] ] + = 1
Exercise 3: ScoreKeeper_YI.py
You are a score keeper for 20 ice hockey players. Implement the code to keep track of their scores using a list. Program accepts integers as input and it is called ScoreKeeper_YI.py. Simulate a game by entering a good number of scores.
Sample output:
>>> ... the game is on!!! what player scored a goal? 3 what player scored a goal? 5 what player scored a goal? 3 what player scored a goal? 3 what player scored a goal? 5 still game on?y/n y what player scored a goal? 5 what player scored a goal? 5 what player scored a goal? 5 what player scored a goal? 4 what player scored a goal? 6 still game on?y/n y what player scored a goal? 5 what player scored a goal? 5 what player scored a goal? 7 what player scored a goal? 8 what player scored a goal? 1 still game on?y/n y what player scored a goal? 5 what player scored a goal? 5 what player scored a goal? 3 what player scored a goal? 2 what player scored a goal? 5 still game on?y/n n Creating a histogram from players: Player Goals Histogram 1 1 * 2 1 * 3 4 **** 4 1 * 5 10 ********** 6 1 * 7 1 * 8 1 * 9 0 10 0 11 0 12 0 13 0
Exercise 4: BingoNumKeeper_YI.py
You are in charge of keeping track of the numbers that are called out in a Bingo game. Using a list implement the code to keep track of the numbers. The numbers will be called until the board is completely filled up. You can think of this simulation as if the balls are replaced back for drawing again. In this program use the random feature to simulate turning the spherical basket with all the balls inside. It is similar to the game of Bingo but not quite. At the end of the program print the number of times a random number was called with a meaningful message. Name your program BingoNumKeeper_YI.py