Ckrielle
Ckrielle

Reputation: 64

How to format an argument inside a function but also print the string as many times as the argument

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

Answers (1)

ecc521
ecc521

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

Related Questions