Discussion:
[Tutor] creating a dictionary for capital quiz program
Stephanie Quiles
2015-06-01 23:19:41 UTC
Permalink
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.

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()



_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.o
Peter Otten
2015-06-02 07:43:17 UTC
Permalink
Post by Stephanie Quiles
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.
state = input('Enter the capital of '+k+' :')
right += 1
print('Correct')
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.
Post by Stephanie Quiles
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 - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tu
Joel Goldstick
2015-06-02 12:49:56 UTC
Permalink
Post by Peter Otten
Post by Stephanie Quiles
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.
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 + ':')
Post by Peter Otten
Post by Stephanie Quiles
right += 1
print('Correct')
wrong += 1
print('Incorrect')
When and how often is the line
executed?
Hint: look at the indentation in the quoted code.
Post by Stephanie Quiles
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.
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
--
Joel Goldstick
http://joelgoldstick.com
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://
ZBUDNIEWEK.JAKUB
2015-06-02 08:45:57 UTC
Permalink
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=***@python.org] On Behalf Of Peter Otten
Sent: Tuesday, June 02, 2015 9:43 AM
To: ***@python.org
Subject: Re: [Tutor] creating a dictionary for capital quiz program
Post by Stephanie Quiles
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.
state = input('Enter the capital of '+k+' :')
right += 1
print('Correct')
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.
Post by Stephanie Quiles
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 - ***@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.
************************************************************************************
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/m
Peter Otten
2015-06-02 14:15:15 UTC
Permalink
Post by ZBUDNIEWEK.JAKUB
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.
Post by ZBUDNIEWEK.JAKUB
input("? ")
? 1 + 1
2
Post by ZBUDNIEWEK.JAKUB
raw_input("? ")
? 1 + 1
'1 + 1'

Python 3 has no raw_input() and input() will behave like raw_input() in
Python 2.
Post by ZBUDNIEWEK.JAKUB
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.
Post by ZBUDNIEWEK.JAKUB
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'}
state = input('Enter the capital of '+k+' :')
right += 1
print('Correct')
wrong += 1
print('Incorrect')
choice = input('Do you want to play again y/n: ')
print('end of game')
break
print("invalid choice")
print('Number of correct answers is: ', right)
print("Number of incorrect answers is:", wrong)
main()
Regards,
Jakub
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Stephanie Quiles
2015-06-02 14:50:22 UTC
Permalink
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
Post by Peter Otten
Post by ZBUDNIEWEK.JAKUB
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
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.
Post by ZBUDNIEWEK.JAKUB
input("? ")
? 1 + 1
2
Post by ZBUDNIEWEK.JAKUB
raw_input("? ")
? 1 + 1
'1 + 1'
Python 3 has no raw_input() and input() will behave like raw_input() in
Python 2.
Post by ZBUDNIEWEK.JAKUB
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
...
Not an optimization, but if the user enters neither Y nor N you might ask
again instead of assuming Y.
Post by ZBUDNIEWEK.JAKUB
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'}
state = input('Enter the capital of '+k+' :')
right += 1
print('Correct')
wrong += 1
print('Incorrect')
choice = input('Do you want to play again y/n: ')
print('end of game')
break
print("invalid choice")
print('Number of correct answers is: ', right)
print("Number of incorrect answers is:", wrong)
main()
Regards,
Jakub
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-06-02 15:38:08 UTC
Permalink
Post by Stephanie Quiles
So basically, what I did wrong was the indentation?
Yes.
In Python indentation is all important.

When you write a for (or while) loop Python executes all the
indented code under the opening loop statement. When it sees
an unindented statement it reads that as the next line
to execute *after* the loop completes.
--
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


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-06-02 15:45:54 UTC
Permalink
Post by Peter Otten
Not an optimization, but if the user enters neither Y nor N you might ask
again instead of assuming Y.
He does. He only breaks if the user enters N
Post by Peter Otten
Post by Stephanie Quiles
choice = input('Do you want to play again y/n: ')
print('end of game')
break
print("invalid choice")
Y goes round again silently.
Anything other than Y or N prints the error then tries again.
--
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


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Peter Otten
2015-06-02 16:17:09 UTC
Permalink
Post by Alan Gauld
Post by Peter Otten
Not an optimization, but if the user enters neither Y nor N you might ask
again instead of assuming Y.
He does. He only breaks if the user enters N
Post by Peter Otten
Post by Stephanie Quiles
choice = input('Do you want to play again y/n: ')
print('end of game')
break
print("invalid choice")
Y goes round again silently.
Anything other than Y or N prints the error then tries again.
... with the next state. I meant that instead the question "Do you want to
play again y/n:" should be repeated until there is a valid answer, either y
or n.

Current behaviour:

$ python capitals.py
Enter the capital of Mississippi :Jackson
Correct
Do you want to play again y/n: x
invalid choice
Enter the capital of Oklahoma :
...

So "x" is a synonum for "n".

Suggested behaviour:

$ python capitals.py
Enter the capital of Mississippi :Jackson
Correct
Do you want to play again y/n: x
invalid choice
Do you want to play again y/n: z
invalid choice
Do you want to play again y/n: n
end of game
...


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Stephanie Quiles
2015-06-02 16:25:25 UTC
Permalink
What is the +k+ called? How exactly does it work? I'm a big confused on that...

Stephanie Quiles
Sent from my iPhone
Post by Peter Otten
Post by Alan Gauld
Post by Peter Otten
Not an optimization, but if the user enters neither Y nor N you might ask
again instead of assuming Y.
He does. He only breaks if the user enters N
Post by Peter Otten
Post by Stephanie Quiles
choice = input('Do you want to play again y/n: ')
print('end of game')
break
print("invalid choice")
Y goes round again silently.
Anything other than Y or N prints the error then tries again.
... with the next state. I meant that instead the question "Do you want to
play again y/n:" should be repeated until there is a valid answer, either y
or n.
$ python capitals.py
Enter the capital of Mississippi :Jackson
Correct
Do you want to play again y/n: x
invalid choice
...
So "x" is a synonum for "n".
$ python capitals.py
Enter the capital of Mississippi :Jackson
Correct
Do you want to play again y/n: x
invalid choice
Do you want to play again y/n: z
invalid choice
Do you want to play again y/n: n
end of game
...
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-06-02 18:39:52 UTC
Permalink
Post by Stephanie Quiles
What is the +k+ called? How exactly does it work? I'm a big confused on that...
You seem to be replying to the wrong post.

I assume you mean this one from Joel?

-----------------
<big snip>
Post by Stephanie Quiles
Post by Stephanie Quiles
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 + ':')
<snip>
------------------

The '+k+' isn't called anything per s. It's an example of
string concatenation, and the only difference in Joel's rendition
is the spacing, to clarify what's going on. (He may have misread
your original code and thought it was in error?)

Joel's version is easier to read, although it does lose
some spaces in the prompt string.

FWIW You could clarify it further by using better variable
names such as:

for state in capitals.keys():
guess = input('Enter the capital of ' + state +' :')

Or you could have used string formatting:

for state in capitals.keys():
guess = input('Enter the capital of %s:' % state)

HTH
--
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


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-06-02 18:30:31 UTC
Permalink
Post by Peter Otten
Post by Alan Gauld
Post by Stephanie Quiles
choice = input('Do you want to play again y/n: ')
print('end of game')
break
print("invalid choice")
Y goes round again silently.
Anything other than Y or N prints the error then tries again.
... with the next state. I meant that instead the question "Do you want to
play again y/n:" should be repeated until there is a valid answer, either y
or n.
OK, I agree that it's a rad unconventional.
I thought you thought he was just quitting with the error message.
Post by Peter Otten
$ python capitals.py
Enter the capital of Mississippi :Jackson
Correct
Do you want to play again y/n: x
invalid choice
Do you want to play again y/n: z
invalid choice
Yes this would be the normal idiom.
--
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


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-06-02 15:43:38 UTC
Permalink
Post by ZBUDNIEWEK.JAKUB
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?
Code can nearly always be optimised.
Whether it is worth doing so depends on the need to do so.
In this case I douybt its worthwhile :-)
Post by ZBUDNIEWEK.JAKUB
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')?
As Peter has said, you are probably running Python 2 rather
than Python 3. input() changed behaviour in the upgrade.
Post by ZBUDNIEWEK.JAKUB
right = 0
wrong = 0
capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau', "Arizona": 'Phoenix', \
'Arkansas': 'Little Rock', 'California': 'Sacramento', \
You don't need the backslashes. Python is quite happy to read

capitals = {'Alabama': 'Montgomery', 'Alaska': 'Juneau',
"Arizona": 'Phoenix', 'Arkansas': 'Little Rock',
'California': 'Sacramento', ...etc...
}

So long as its inside (),{}, or {} (or triple quotes,
although they are slightly different) you don;t need line
continuation marks (\).
Post by ZBUDNIEWEK.JAKUB
state = input('Enter the capital of '+k+' :')
right += 1
print('Correct')
wrong += 1
print('Incorrect')
choice = input('Do you want to play again y/n: ')
print('end of game')
break
print("invalid choice")
print('Number of correct answers is: ', right)
print("Number of incorrect answers is:", wrong)
--
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


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Continue reading on narkive:
Loading...