| | |
loop problem
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 12
Reputation:
Solved Threads: 0
# Reading the .csv file
energyFile = open("EnergySources.csv","r")
stateDict = {}
for line in energyFile:
line = line.strip()
fields = line.split(",")
if fields[0] == "State" or fields[0] == "":
continue
state = fields[0]
total = fields[-1]
float(''.join(total))
coal = fields[1]
float(''.join(coal))
naturalgas = fields[2]
float(''.join(naturalgas))
petroleum = fields[3]
float(''.join(petroleum))
nuclear = fields[4]
float(''.join(nuclear))
hydroelectric = fields[5]
float(''.join(hydroelectric))
biomass = fields[6]
float(''.join(biomass))
geothermal = fields[7]
float(''.join(geothermal))
interstate = fields[8]
float(''.join(interstate))
other = fields[9]
float(''.join(other))
Statename = state
stateDict[Statename] = coal,naturalgas,petroleum,nuclear,hydroelectric,biomass,geothermal,interstate,other
while True:
query = raw_input('''
1:Coal
2:Natural gas
3:Petroleum
4:Nuclear
5:Hydroelectric
6:Biomass
7:Geothermal
8:Interstate imports
\nPick an energy source (ex.2): ''')
if query not in ("1","2","3","4","5","6","7","8"):
print ("Not a valid choice")
else:
query2=int(query)
query1 = raw_input("Pick a state (ex.California): ")
# Check to see if it's coal
if query1 in stateDict and query2==1:
# extract the data from the database
print "Coal:",stateDict[query1][0]
#Check to see if it's natural gas
elif query1 in stateDict and query2==2:
# extract the data from the database
print "Natural Gas:",stateDict[query1][1]
#Check to see if it's petroleum
elif query1 in stateDict and query2==3:
# extract the data from the database
print stateDict[query1][2]
#Check to see if it's nuclear
elif query1 in stateDict and query2==4:
# extract the data from the database
print stateDict[query1][3]
#Check to see if it's hydroelectric
elif query1 in stateDict and query2==5:
# extract the data from the database
print stateDict[query1][4]
#Check to see if it's biomass
elif query1 in stateDict and query2==6:
# extract the data from the database
print stateDict[query1][5]
#Check to see if it's geothermal
elif query1 in stateDict and query2==7:
# extract the data from the database
print stateDict[query1][6]
#Check to see if it's interstate
elif query1 in stateDict and query2==8:
# extract the data from the database
print stateDict[query1][7]
else:
print "\nNot Valid Input. Please Try Again!"
energyFile.close()at the part where it's bolded im trying to make it loop back to ask the question of the user does not input the numbers 1-9 but when user input 10 it just says not a valid input and it just goes down to the next loop. can someone help me fix this probelm so that it goes back up and ask the same question until it gets 1-9?
![]() |
Similar Threads
- perl loop problem (from a beginner) (Perl)
- Infinite loop problem need help plz (PHP)
- For loop problem (Java)
- Strange Excel infinite-loop problem (Windows Software)
- a for loop problem (Java)
- Alright this loop problem is bugging me (Java)
- I think Loop problem (Java)
- loop problem (C++)
Other Threads in the Python Forum
- Previous Thread: Python3.1.1 can't assign function result to variable
- Next Thread: Pumpkin Weight Help
Views: 157 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for Python
anti approximation array avogadro beginner builtin cipher clear client code color converter countpasswordentry cturtle curved def dictionary drive dynamic examples excel file float format frange ftp function gui homework import input java lapse library line lines linux list lists loop microcontroller mouse multiple mysqldb mysqlquery newb number numbers output parsing path port prime program programming projects py2exe pygame pymailer pyqt python random recursion recursive redirect script scrolledtext singleton socket sqlite ssh stderr string strings subprocess sum syntax table terminal text textarea thread threading time tkinter tlapse tuple tutorial twoup ubuntu unicode unix urllib urllib2 variable web-scrape wikipedia windows word wxpython





