[Tutor] role playing game - help needed
ALAN GAULD
alan.gauld at btinternet.com
Thu Dec 9 10:09:51 CET 2010
Can you explain this in a little more detail?
>
>
sure.
>
>for name in attributes.keys():
> attributes[name] = int( input("How many points do you want to assign to %s " %
>name) )
>
>Where did you get 'name' from?
>
I made it up. The for loop takes the form
for <variable name3> in <some collection>:
<some action>
where the bits in <> are provided by the programmer.
name was just a "meaningful" variable name that I chose.
> What does the % do and how does it work inside the
> quotation marks as opposed to outside?
This is called string formatting. Actually in Python 3 there is a new way of
doing this but the older style, like this, still works.
Basically within the string we insert percent signs followed by
a special letter to indicate the type of data we want to insert into
the string. %s means a string, %d a decimal number, %f a floating
point number etc.
The % immediately after the string is a separator preceding the
values to be inserted into the string
Try it at the >>> prompt:
>>> "%d is a number" % 6
6 is a number
>>> "%d is the result of %d + %d" % (6+7,6,7)
13 is the result of 6 + 7
>>> "My name is %s" % "Alan"
My name is Alan
Someone else might show you the v3 way using the
new string.format() operator
HTH,
Alan G.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101209/46c2b8b2/attachment-0001.html>
More information about the Tutor
mailing list