5 Replies - 156 Views - Last Post: 17 July 2014 - 10:58 AM Rate Topic: -----

#1 flyboynm  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 121
  • Joined: 04-December 11

code won't exit out of loop

Posted 17 July 2014 - 09:55 AM

I am trying to do this:

Write a program that creates a directory containing the US States as keys and their capitals
as values. The program should then randomly quiz the user to enter that state's capital. The
program should then randomly quiz the by displaying the name of the state and asking the user
to enter the capital. The program should keep track of the correct and incorrect entries.


import random

again = 1
quit = 2

def main():
    
    
    capitals={"Washington":"Olympia","Oregon":"Salem",\
                    
              "California":"Sacramento","Ohio":"Columbus",\
              
              "Nebraska":"Lincoln","Colorado":"Denver",\

              "Michigan":"Lansing","Massachusetts":"Boston",\

              "Florida":"Tallahassee","Texas":"Austin",\

              "Oklahoma":"Oklahoma City","Hawaii":"Honolulu",\

              "Alaska":"Juneau","Utah":"Salt Lake City",\

              "New Mexico":"Santa Fe","North Dakota":"Bismarck",\

              "South Dakota":"Pierre","West Virginia":"Charleston",\

              "Virginia":"Richmond","New Jersey":"Trenton",\

              "Minnesota":"Saint Paul","Illinois":"Springfield",\

              "Indiana":"Indianapolis","Kentucky":"Frankfort",\

              "Tennessee":"Nashville","Georgia":"Atlanta",\

              "Alabama":"Montgomery","Mississippi":"Jackson",\

              "North Carolina":"Raleigh","South Carolina":"Columbia",\

              "Maine":"Augusta","Vermont":"Montpelier",\

              "New Hampshire":"Concord","Connecticut":"Hartford",\

              "Rhode Island":"Providence","Wyoming":"Cheyenne",\

              "Montana":"Helena","Kansas":"Topeka",\

              "Iowa":"Des Moines","Pennsylvania":"Harrisburg",\

              "Maryland":"Annapolis","Missouri":"Jefferson City",\

              "Arizona":"Phoenix","Nevada":"Carson City",\

              "New York":"Albany","Wisconsin":"Madison",\

              "Delaware":"Dover","Idaho":"Boise",\

              "Arkansas":"Little Rock","Louisiana":"Baton Rouge"}

    
    correct = 0

    incorrect = 0

    print ('What is the state capital quiz\n')
    
    choice = input('To take the quiz, enter 1, to quit, enter 2')

    while choice != 2:

        pick=random.choice(list(capitals.keys()))

        correct_answer=capitals.get(pick)

        print ("What is the capital city of ",pick,"?", sep='')

        answer = input("Your answer: ")

        if answer.lower()==correct_answer.lower():

            print ("That's Correct!\n")

            correct +=1

        else:

            print ("That's Incorrect.")

            print ("The correct answer is",correct_answer)

            incorrect +=1
        print('Do you want to continue?')

        choice = input('1 for yes, 2 for no.')

    
    print ("You got ", correct " right and you missed", incorrect, ".")

main()




What am I doing wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: code won't exit out of loop

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3358
  • View blog
  • Posts: 11,371
  • Joined: 12-December 12

Re: code won't exit out of loop

Posted 17 July 2014 - 10:14 AM

input() returns a string, so this would work:
    while choice != '2':

although it would be preferable to convert the choice to an integer (int()) IMO.

This post has been edited by andrewsw: 17 July 2014 - 10:15 AM

Was This Post Helpful? 1
  • +
  • -

#3 flyboynm  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 121
  • Joined: 04-December 11

Re: code won't exit out of loop

Posted 17 July 2014 - 10:22 AM

I keep doing that! *bangs head against a wall*

Is there a way to del a state out of the list once it has been done? Would I put it in the while loop? Something like:

del capitals
Was This Post Helpful? 0
  • +
  • -

#4 flyboynm  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 121
  • Joined: 04-December 11

Re: code won't exit out of loop

Posted 17 July 2014 - 10:30 AM

Forget it - I am going to be happy with what I have done. :)
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3358
  • View blog
  • Posts: 11,371
  • Joined: 12-December 12

Re: code won't exit out of loop

Posted 17 July 2014 - 10:33 AM

del capitals would delete the entire dictionary. Instead
del capitals[key]

Personally, I would probably create a list (or other collection) to populate as the questions are answered. Then you could check this list to ensure that the same question is not asked twice.

But, yes, removing the keys will also work.




I would probably also use state-codes (WA) which seem better suited as keys (rather than text containing spaces), but this isn't indicated, or suggested, by your statement of the assignment.
Was This Post Helpful? 1
  • +
  • -

#6 flyboynm  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 121
  • Joined: 04-December 11

Re: code won't exit out of loop

Posted 17 July 2014 - 10:58 AM

Thanks Andrew. I am doing this on my own so it is the assignments I am giving myself. For me, I am just going to call it a day and go to the next one....and I can already feel the headache coming. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1