Reputation: 11
I'm trying to make a rectangle out of a given symbol with a specific given height and width. The first def function should contain only one parameter. This should be a higher-order function as well. This is my code so far:
def rectangle(x):
def someName(width,height):
i = x * width
while len(i) <= height:
print(i)
i = i+1
return someName
If entering something like this to test it, this is how it should print:
>>> percent = rectangle('%')
>>> dot = rectangle('.')
>>> print(percent(3,2))
%%%
%%%
>>> print(dot(4,1))
....
My code currently returns None
. How do I get it to return i
? If I change return someName
to return i
, many other errors occur.
Upvotes: 1
Views: 1565
Reputation: 1080
You can try to use the decorator function of python,like this:
def decorate(key):
def real_decorate(func):
def wrapper(width, height):
return func(width * key, height)
return wrapper
return real_decorate
@decorate("%")
def rectangle(width, height):
return [width for i in range(height)]
print("\n".join(rectangle(10, 5)))
Upvotes: 0
Reputation: 361739
someName
is printing the characters, so calling print
from the outside is redundant. If you want someName
to do the printing then change the calls to:
>>> percent(3,2)
>>> dot(4,1)
On the other hand, if you want caller to call print
then you'll need to change someName
to build up a string piece by piece and return it at the end, instead of doing the printing itself. For example:
def someName(width,height):
i = x * width
lines = []
while len(i) <= height:
lines.append(i)
i = i+1
return '\n'.join(lines)
Upvotes: 1