loop problem

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

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

loop problem

 
0
  #1
Nov 2nd, 2009
# 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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 12
Reputation: vnproduktionz is an unknown quantity at this point 
Solved Threads: 0
vnproduktionz vnproduktionz is offline Offline
Newbie Poster
 
0
  #2
Nov 3rd, 2009
nvm i got it
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 156 | Replies: 1
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC