Question:
n = int(input())
peng =( \
"""
_~_
(o o)
/ V \
/( _ )\
^^ ^^
""")
if n == 1:
print(str(peng))
elif n == 2:
print(peng*2)
elif n == 3:
print(peng*3)
elif n == 4:
print(peng * 4)
elif n == 5:
print(peng*5)
elif n == 6:
print(peng * 6)
elif n == 7:
print(peng * 7)
elif n == 8:
print(peng * 8)
elif n == 9:
print(peng * 9)
How can I make sure that the pictures are displayed in one line, and not wrapped? And yes, I am aware that the same can be written better using for_ in
.
Answer:
The algorithm is as follows: we split the "picture" into separate lines, duplicate each line the required number of times, then assemble the lines back into a whole picture.
peng =(
r"""
_~_
(o o)
/ V \
/( _ )\
^^ ^^
""")
def mul_image(img, n):
return '\n'.join(line * n for line in img.splitlines())
print(mul_image(peng, 3))
Conclusion:
_~_ _~_ _~_
(o o) (o o) (o o)
/ V \ / V \ / V \
/( _ )\ /( _ )\ /( _ )\
^^ ^^ ^^ ^^ ^^ ^^
In order for everything to be displayed beautifully, all lines of the picture (where there are printable characters) must be of the same length (i.e. spaces at the end of the lines matter). In the picture in the question, there are no additional spaces after the "paws", and if you output through the function above, you get this:
_~_ _~_ _~_
(o o) (o o) (o o)
/ V \ / V \ / V \
/( _ )\ /( _ )\ /( _ )\
^^ ^^ ^^ ^^ ^^ ^^
In principle, the function can be modified so that all lines are spaced to the length of the longest of the lines, then trailing spaces can be ignored:
def mul_image(img, n):
lines = img.splitlines()
max_len = max(len(line) for line in lines)
return '\n'.join(line.ljust(max_len, ' ') * n for line in lines)