![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2005
Posts: 2
Rep Power: 0
![]() |
help with a program
Write a Python program to do the following.
You wish to store, maintain, update, and use data for each of several baseball players. For each player you will store last name, player number, number of times at bat, and number of hits. Use a nested list to store this data. Your program will have the following major parts. 1. Create the data structure and populate it with data. For this part, you should read the data from a text file named “players.txt”. You do not know how many total lines of data are in this file, but you DO know that you have 4 lines for each player. Furthermore, you also know that for each player, the data in the file is given in the order: last name, player number, at bats, hits. Finally, all data is of the expected type. (In other words, there are no “tricks” in the data file.) 2. Update the data in your data structure. This would correspond to the appropriate updates at the end of a game. For this part, you should go through your list of players and do the following for each player: print the name and player number to the screen, ask the user to input the number of at-bat appearances that game, ask the user to input the number of hits for that game, and then update the player’s record accordingly. 3. Count and output the number of players who have a batting average of 300 or higher. Note that batting average is not stored directly. You will have to compute it. Do NOT store it in the player’s record. 4. Count and output the number of players in each of the following classifications. 0-50 hits whiffer 51-100 hits minor 101-150 hits productive over 150 hits MVP candidate You must design your code so that you make good use of functions. At a very minimum, you should have a function for each of the parts described above. You may, of course, use more functions than this. I am having trouble with #1 and making a list into sublists. Any help would be awesome. Thanks, Mike |
|
|
|
|
|
#2 |
|
Expert Programmer
|
Let's see some work first.
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Feb 2005
Posts: 24
Rep Power: 0
![]() |
I'm not doing your homework for you am I? A few hints...
[ [], [], [], [] ] The above code creates a nested list, call it 'l'. Imagine the list indexed by l[0] is used to store player surnames. To add a name to this sublist, do l[0].append(foo). To me, though, it'd make more sense to create a dictionary for each player with 4 keys (last name, player no., no. of times at bat, no. of hits). Btw, this stuff really isn't complicated. You should be able to work it out for yourself from an online tutorial. Or just buy a book. Last edited by al1986; Apr 3rd, 2005 at 8:50 PM. |
|
|
|
|
|
#4 |
|
Newbie
Join Date: Apr 2005
Posts: 2
Rep Power: 0
![]() |
Ok, well here's some work. As you can see I can get all the things into sublists, but not with four objects in each sublist.
def dataList(): import string list = [] InFile = open("players.txt" , "r") for line in InFile: words = string.split(line) list.append(words) print list results in this... >>> [['A'], ['7'], ['100'], ['33'], [], ['B'], ['5'], ['150'], ['50'], [], ['C'], ['2'], ['200'], ['66'], [], ['D'], ['3'], ['500'], ['145']] >>> |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|