944,198 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 251
  • Python RSS
Nov 2nd, 2009
0

loop problem

Expand Post »
# 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vnproduktionz is offline Offline
12 posts
since Oct 2009
Nov 3rd, 2009
0
Re: loop problem
nvm i got it
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vnproduktionz is offline Offline
12 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Python3.1.1 can't assign function result to variable
Next Thread in Python Forum Timeline: Pumpkin Weight Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC