0

I am trying to loop this 10 times and change all the the numbers on each line.

<li>1. <a href='/some/url/index.php?member=_MEMBER_1_'>_MEMBER_1_DESC_ (_MEMBER_1_UNI_IN_) </a></li>

I tried it with range function but dont know how to change the digits.

#!/usr/bin/python
for i in range(5):
print  ("<li> %d. <a href='/some/url/index.php?member=_MEMBER_%d_'>_MEMBER_%d_DESC_ (_MEMBER_%d_UNI_IN_) </a></li>") % i

maybe I have to set a regex for the line and then filter on numbers. but then how to change them?

2 Answers 2

3

Consider:

>>> "%d%d%d" % 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string
>>> "%d%d%d" % (1,1,1)
'111'

You need to specify a number for each '%d' in your string.

Note that you can get around this if you use the dictionary form:

>>> "%(val)s%(val)s%(val)s" % {'val': 1}
'111'

But these days I would prefer .format:

>>> '{0}{0}{0}'.format(1)
'111'

In your case:

print ("<li> {0}. <a href='/some/url/index.php?member=_MEMBER_{0}_'>_MEMBER_{0}_DESC_ (_MEMBER_{0}_UNI_IN_) </a></li>").format(i)
Sign up to request clarification or add additional context in comments.

Comments

0

You must do:

for i in range(5):
    print  ("<li> %d. <a href='/some/url/index.php?member=_MEMBER_%d_'>_MEMBER_1_DESC_ (_MEMBER_%d_UNI_IN_) </a></li>") % **(i, i, i)**

Note the 3 times that I put the "i" variable.

And if you want to make the loop 10 times, assuming you need number from 1 to 10, you should use: for i in range(1, 11): print ...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.