[Tutor] creat a program that reads frequency of words in file

Stephanie Quiles stephanie.quiles001 at albright.edu
Tue Jun 2 03:35:16 CEST 2015


thanks on the help. I am now stuck on this program for quizzing on state capitals. Do you mind taking a look please? I can’t get it to tell me the answer is incorrect it just keeps asking me for capitals whether the answer is right or wrong. It also is not giving me correct counts for correct and incorrect answers. Any help would be appreciated. Not sure if i turned HTML. my laptop is fairly new and I’m still assimilating to iOS. Please let me know if the code is hard to read. 

Thanks 

_______________________________________________________________________________
Write a program that creates a dictionary containing the U.S. States as keys and their
capitals as values.
(Use the internet to get a list of the states and their capitals.)
The program should then randomly quiz the user by displaying the name of a state and asking
the usr to enter that state's capital.
The program should keep a count of the number of correct and incorrect responses.
(As an alternative to the US states, the program can use the names of countries and
their capitals.)"""

import pickle


def main():
    right = 0
    wrong = 0
    capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau', \
 \
               "Arizona": 'Phoenix', \
 \
               'Arkansas': 'Little Rock', 'California': 'Sacramento', \
 \
               'Colorado': 'Denver', \
 \
               'Connecticut': 'Hartford', 'Delaware': 'Dover', \
 \
               'Florida': 'Tallahassee', \
 \
               'Georgia': 'Atlanta', 'Hawaii': 'Honolulu', \
 \
               'Idaho': 'Boise', \
 \
               'Illinois': 'Springfield', 'Indiana': 'Indianapolis', \
 \
               'Iowa': 'Des Moines', \
 \
               'Kansas': 'Topeka', 'Kentucky': 'Frankfort', \
 \
               'Louisiana': 'Baton Rouge', \
 \
               'Maine': 'Augusta', 'Maryland': 'Annapolis', \
 \
               'Massachusetts': 'Boston', \
 \
               'Michigan': 'Lansing', 'Minnesota': 'Saint Paul', \
 \
               'Mississippi': 'Jackson', \
 \
               'Missouri': 'Jefferson City', 'Montana': 'Helena', \
 \
               'Nebraska': 'Lincoln', \
 \
               'Nevada': 'Carson City', 'New Hampshire': 'Concord', \
 \
               'New Jersey': 'Trenton', \
 \
               'New Mexico': 'Santa Fe', 'New York': 'Albany', \
 \
               'North Carolina': 'Raleigh', \
 \
               'North Dakota': 'Bismarck', 'Ohio': 'Columbus', \
 \
               'Oklahoma': 'Oklahoma City', \
 \
               'Oregon': 'Salem', 'Pennsylvania': 'Harrisburg', \
 \
               'Rhode Island': 'Providence', \
 \
               'South Carolina': 'Columbia', \
 \
               'South Dakota': 'Pierre', 'Tennessee': 'Nashville', \
 \
               'Texas': 'Austin', 'Utah': 'Salt Lake City', \
 \
               'Vermont': 'Montpelier', \
 \
               'Virginia': 'Richmond', 'Washington': 'Olympia', \
 \
               'West Virginia': 'Charleston', \
 \
               'Wisconsin': 'Madison', 'Wyoming': 'Cheyenne'}

    for k in capitals.keys():
        state = input('Enter the capital of '+k+' :')
    if state.upper() == capitals[k].upper():
        right += 1
        print('Correct')
    else:
        wrong += 1
        print('Incorrect')
    choice = input('Do you want to play again y/n: ')
    if choice.upper() == 'N':
        print('end of game')
    else:
        choice.upper() != 'Y'
        print("invalid choice")

    print('Number of correct answers is: ', right)
    print("Number of incorrect answers is:", wrong)

main()



> On Jun 1, 2015, at 7:42 PM, Alan Gauld <alan.gauld at btinternet.com> wrote:
> 
> I've CCd the list. Please use reply all when responding to the list.
> Also please use plain text as HTML/RTF doesn't work on all
> systems and code in particular often gets mangled.
> 
> On 01/06/15 23:59, Stephanie Quiles wrote:
>> Hello again,
>> 
>> here is the final code… I think :) please see below. Is this is the easiest way to go about it? I appreciate your assistance!
>> 
>> defmain():
>>     words = {}
>>     count =0
> 
> Do you need count? What is its purpose?
>>     withopen('words.txt')asdata:
>>         forlineindata:
>>             text = line.split()
>>             forwordintext:
>>                 ifwordnot inwords:
>>                     words[word] =1
>>                 else:
>>                     words[word] +=1
> 
> Look into the setdefault() method of dictionaries.
> It can replace the if/else above.
> 
>>             count +=1
>>     print(words)
> 
> Aside from the two comments above, good job!
> 
> -- 
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
> 



More information about the Tutor mailing list