[Tutor] creating a dictionary for capital quiz program

Stephanie Quiles stephanie.quiles001 at albright.edu
Tue Jun 2 16:50:22 CEST 2015


Thank you all for your help! I have a text today but I am not confident with this. So basically, what I did wrong was the indentation? 

Thanks 

Stephanie Quiles
Sent from my iPhone

> On Jun 2, 2015, at 10:15 AM, Peter Otten <__peter__ at web.de> wrote:
> 
> ZBUDNIEWEK. JAKUB wrote:
> 
>> I'm a newbie, but was able to tune it to correctly reply to user inputs.
> 
>> 2. Why (on Windows) do I have to give inputs in quotes not to cause an
>> error (for ll input the error is ' NameError: name 'll' is not defined')?
> 
> If you are running the script under Python 2 you should use
> raw_input() instead of input(). input() will take the user input and also 
> run eval() on it:
> 
> Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
> [GCC 4.8.2] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> input("? ")
> ? 1 + 1
> 2
>>>> raw_input("? ")
> ? 1 + 1
> '1 + 1'
> 
> Python 3 has no raw_input() and input() will behave like raw_input() in 
> Python 2.
> 
>> 1. My question is can it be optimized in any way?
> 
> In Python 2 capital.keys() builds a list. You can avoid that by iterating 
> over the dict directly:
> 
> for k in capitals:
>    ...
> 
> Not an optimization, but if the user enters neither Y nor N you might ask 
> again instead of assuming Y.
> 
> 
>> 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')
>>            break
>>        elif choice.upper() != 'Y':
>>            print("invalid choice")
>> 
>>    print('Number of correct answers is: ', right)
>>    print("Number of incorrect answers is:", wrong)
>> 
>> main()
>> 
>> Regards,
>> Jakub
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list