Reputation: 64
I am a beginner and was just messing around with functions and i thought of the code below. I want to print the strings as many times as whatever the value of a is going to be, but i also want to format the value of the argument a inside the string. Any help will be greatly appreciated!
def vhf(a):
print "So i want this times %d "*a % a
vhf(5)
When I run it I get this error:
Traceback (most recent call last):
File "p.py", line 4, in <module>
vhf(5)
File "p.py", line 2, in vhf
print "...So i want this times %d "*a % a
TypeError: not enough arguments for format string
Upvotes: 0
Views: 138
Reputation: 657
When you multiply the string by a, the number of format arguments needed is multiplied by a. You can move the multiplication to AFTER the formatting to fix your issue.
def vhf(a):
print "So i want this times %d " % a * a
vhf(5)
Upvotes: 2