[Tutor] creating a dictionary for capital quiz program

Peter Otten __peter__ at web.de
Tue Jun 2 09:43:17 CEST 2015


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+' :')
>     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.





More information about the Tutor mailing list