[Tutor] creating a dictionary for capital quiz program
ZBUDNIEWEK.JAKUB
JAKUB.ZBUDNIEWEK at arimr.gov.pl
Tue Jun 2 10:45:57 CEST 2015
I'm a newbie, but was able to tune it to correctly reply to user inputs.
1. My question is can it be optimized in any way?
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')?
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
-----Original Message-----
From: Tutor [mailto:tutor-bounces+jakub.zbudniewek=arimr.gov.pl at python.org] On Behalf Of Peter Otten
Sent: Tuesday, June 02, 2015 9:43 AM
To: tutor at python.org
Subject: Re: [Tutor] creating a dictionary for capital quiz program
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.
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
************************************************************************************
Wiadomość ta jest przeznaczona jedynie dla osoby lub podmiotu, który jest jej adresatem i
może zawierać poufne i/lub uprzywilejowane informacje. Zakazane jest jakiekolwiek
przeglądanie, przesyłanie, rozpowszechnianie lub inne wykorzystanie tych informacji lub
podjęcie jakichkolwiek działań odnośnie tych informacji przez osoby lub podmioty inne niż
zamierzony adresat. Jeżeli Państwo otrzymali przez pomyłkę tę informację prosimy o
poinformowanie o tym nadawcy i usunięcie tej wiadomości z wszelkich komputerów.
--------------------------------------------------------------------------------
The information transmitted is intended only for the person or entity to which it is addressed
and may contain confidential and/or privileged material. Any review, retransmission,
dissemination or other use of, or taking of any action in reliance upon, this information by
persons or entities other than the intended recipient is prohibited. If you received this in
error, please contact the sender and delete the material from any computer.
************************************************************************************
More information about the Tutor
mailing list