# 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): ''')
[B]    if query not in ("1","2","3","4","5","6","7","8"):
        print ("Not a valid choice")
    else:
        query2=int(query)[/B]

    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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.