[Tutor] creating a dictionary for capital quiz program

Joel Goldstick joel.goldstick at gmail.com
Tue Jun 2 14:49:56 CEST 2015


On Tue, Jun 2, 2015 at 3:43 AM, Peter Otten <__peter__ at web.de> wrote:
> Stephanie Quiles wrote:
>
>> Good evening,
>>
>> As you may have noticed i am really struggling with functions and
>> dictionaries. I need to figure out why this program is allowing me to
>> continue entering incorrect data instead of telling me my answer is
>> incorrect. also at the end it’s not tallying the incorrect/correct
>> responses properly. please any help would be appreciated.
>
>>     for k in capitals.keys():
>>         state = input('Enter the capital of '+k+' :')

The above statement won't print the name of the capital. try something
like this:
            state = input('Enter the capital of' + k + ':')


>>     if state.upper() == capitals[k].upper():
>>         right += 1
>>         print('Correct')
>>     else:
>>         wrong += 1
>>         print('Incorrect')
>
> When and how often is the line
>
> if state.upper() == capitals[k].upper():
>
> executed?
>
> Hint: look at the indentation in the quoted code.
>
> PS:
>
>>     capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau', \
>>  \
>>                "Arizona": 'Phoenix', \
>>  \
>>                'Arkansas': 'Little Rock', 'California': 'Sacramento', \
>
> You don't need these backslashes as long as you're inside parens, brackets
> or braces. Python will happily accept dicts, lists etc. that spread over
> multiple lines.
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor



-- 
Joel Goldstick
http://joelgoldstick.com


More information about the Tutor mailing list