some help please. (still learning)

Reply

Join Date: Oct 2009
Posts: 11
Reputation: vnproduktionz is an unknown quantity at this point 
Solved Threads: 0
vnproduktionz vnproduktionz is offline Offline
Newbie Poster

some help please. (still learning)

 
0
  #1
25 Days Ago
here's what i currently got
  1. # Reading the .csv file
  2.  
  3. energyFile = open("EnergySources.csv","r")
  4.  
  5. stateDict = {}
  6. for line in energyFile:
  7. line = line.strip()
  8. fields = line.split(",")
  9. if fields[0] == "State" or fields[0] == "":
  10. continue
  11. state = fields[0]
  12.  
  13. total = fields[-1]
  14. float(''.join(total))
  15. coal = fields[1]
  16. float(''.join(coal))
  17. naturalgas = fields[2]
  18. float(''.join(naturalgas))
  19. petroleum = fields[3]
  20. float(''.join(petroleum))
  21. nuclear = fields[4]
  22. float(''.join(nuclear))
  23. hydroelectric = fields[5]
  24. float(''.join(hydroelectric))
  25. biomass = fields[6]
  26. float(''.join(biomass))
  27. geothermal = fields[7]
  28. float(''.join(geothermal))
  29. interstate = fields[8]
  30. float(''.join(interstate))
  31. other = fields[9]
  32. float(''.join(other))
  33.  
  34. for i in range(10):
  35. state1=state[i]
  36.  
  37. while True:
  38. answer= raw_input("Pick a state")
  39. if answer == "":
  40. break
  41. if answer in stateDict:
  42. dataList=state1
  43. state=dataList[0]
  44. print state
  45. else:
  46. print "Not valid"
  47.  
  48.  
  49.  
  50.  
  51.  
  52. energyFile.close()
  53. j

here is the assignment
  1. In this project we will do a data analysis and visualization project, producing maps like the one in the picture above. The map shows millions of BTUs of energy per capits produced by coal in each state in 2007. Why the heck do they burn so much coal in Wyoming?
  2.  
  3. The data comes from two Web sites. The Energy Information Administration is a Federal agency that keeps statistics on energy. The file we're using comes from their overview page. To correct for the populations of the different states, we'll also use the list of state populations from Wikipedia.
  4.  
  5. Finally, we'll actually make the map using IBM's Many Eyes visualization service. This cool Web site lets up upload a table of data, and then choose different ways of visualizing it.
  6.  
  7. The two files we are using are:
  8. EnergySources.csv
  9. StatePopulations.txt
  10. We've taken a preliminary look at both files in class. You do not need to hand in these two files, the TAs will have them when they grade the programs.
  11.  
  12. Your program should read the data from the two files, and write out a third file, sources.tsv, which you should upload to Many Eyes to make the visualization.
  13.  
  14. Working in Pairs
  15.  
  16. You may do this program with a partner. Both you and your partner should hand in the same program. Your partner may be in a different section. Be sure that your program identifies BOTH partners at the top of the file, by name and student ID number. You may do the program by yourself if you want.
  17. If you work with a partner, you should get together, sit down, and work at the same computer. That way you'll both learn the things you'll need to know to pass the tests. If you let your partner do all the work, you will end up failing the midterm (and, probably, your partner will be very annoyed).
  18.  
  19. Steps in the Project
  20.  
  21. We'll do this project in eight steps. After the first four, you'll have a program that is partially working, and you will hand that in on Tues, Nov. 3. On Tues. Nov 10, the whole program, together with your map, will be due. We will only grade the whole program, but if you failed to hand in the partial program, it will cost you points, and if you hand in the partial but not the final program, you can get some partial credit.
  22. The basic structure of the program is the two-loop "build a data structure and do something with it" structure we have been considering in the last two lectures. In these steps, I recommend that you print things out as you go along; none of this printing should appear in the final program, but it will help you figure out whether you have the pieces working as you go along.
  23.  
  24. Step 1: Read in the energy data. Open the file EnergySources.csv, read through it using a for loop (see the example from the 10/26 lecture on the lectures and readings page), print out each line, and then close it.
  25. Step 2: Break each line up and print out first the name of the state, and then each energy source with labels, for instance:
  26.  
  27. Alabama: Coal, 888.4; Natuarl Gas, 431.4; ...
  28.  
  29. Convert the numbers to floating point before printing them out.
  30. Step 3: Modify your program so that instead of printing out the data it puts it into a dictionary. Use the name of the state as the key, and for the data, have a list containing all the sources of energy, and the total. Have your program print out the dictionary, and check it.
  31. Step 4: Write a loop that lets the user ask questions. Offer the user this menu:
  32.  
  33. Pick an energy source:
  34. 1: Coal
  35. 2: Natural gas
  36. 3: Petroleum
  37. 4: Nuclear
  38. 5: Hydroelectric
  39. 6: Biomass
  40. 7: Geothermal
  41. 8: Interstate imports
  42.  
  43.  
  44. Then have them enter the name of a state. In both cases, the program should loop until the user gives a good input.
  45. Step 4: Answer the user's question by looking up the item in the dictionary. Find the list for the requested state, and then the right item in the list.

  1. http://www.cs.ucdavis.edu/~amenta/f09/StatePopulations.txt
  2. http://www.cs.ucdavis.edu/~amenta/f09/ecs10.html

im creating a program that will use my data and organize it in a way that allow user to input something and come up with the answer. i know that i need to make a dictionary list but im not sure how. can i just get some input of where to go and some help
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,008
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 285
woooee woooee is offline Offline
Veteran Poster
 
0
  #2
24 Days Ago
Start with something like this (Not Tested).
  1. energyFile = open("EnergySources.csv","r")
  2. state_dict = {}
  3. for line in energyFile:
  4. line = line.strip()
  5. fields = line.split(",")
  6. state = fields[0].strip()
  7. if state == "State":
  8. ## you didn't give us any idea where state name comes from
  9. state_name = fields[???]
  10. state_dict[state_name] = fields[1:]
  11. elif not len(state):
  12. print "this rec has no state", line
  13. else:
  14. print 'not a "State" record', line
  15.  
  16. answer = ""
  17. while answer not in state_dict:
  18. answer= raw_input("Pick a state")
  19. if answer in state_dict:
  20. data_list=state_dict[answer]
  21. print answer, data_list
  22. print " total is %9.2f" % float(data_list[-1])
  23. else:
  24. print "Not valid"
And for future reference, you will not find many people who will go through a problem with 45 lines to read. State one or two specific problems and you will get better responses.
Last edited by woooee; 24 Days Ago at 11:49 am.
Linux counter #99383
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC