Reputation: 115
How to print the pattern like this:
(need to print n-1 lines)
input=3
----@
--@-@-@
input=6
----------@
--------@-@-@
------@---@---@
----@-----@-----@
--@-------@-------@
My code:
row = int(input())
for i in range(1, row):
for j in range(1,row-i+1):
print("-", end="")
for j in range(1, 2*i):
if j==1 or j==2*i-1:
print("@", end="")
else:
print("-", end="")
print()
MY OUTPUT: input=5
----@
---@-@
--@---@
-@-----@
Please explain how to do??
Upvotes: 0
Views: 135
Reputation: 26
row = int(input())
for i in range(1, row):
for j in range(1,2*(row-i)+1):
print("-", end="")
for j in range(1, 4*i):
if j==1 or j==2*i-1 or j==4*i-3:
print("@", end="")
elif j<=4*i-3:
print("-", end="")
print()
Upvotes: 0
Reputation: 1293
There are a few things missing and to be improved in your code:
"-" * 3 == "---"
"@"
in between."@"
and not three, I prefer to calculate it and print it separately.With these improvements, a solution to your problem could be:
row = int(input())
print("-" * (row - 1) * 2 + "@")
for i in range(row - 2, 0, -1):
left_hyphens = "-" * i * 2
mid_hyphens = "-" * (1 + 2 * (row - 2 - i))
print(left_hyphens + "@" + mid_hyphens + "@" + mid_hyphens + "@")
Upvotes: 2