[Tutor] creating a dictionary for capital quiz program

Alan Gauld alan.gauld at btinternet.com
Tue Jun 2 20:39:52 CEST 2015


On 02/06/15 17:25, Stephanie Quiles wrote:
> 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>
 >>     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 + ':')
<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




More information about the Tutor mailing list