[英]how to print star hypen pattern in python
How to print the pattern like this:如何打印这样的图案:
(need to print n-1 lines ) (需要打印n-1行)
input=3输入=3
----@
--@-@-@
input=6输入=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我的 OUTPUT:输入=5
----@
---@-@
--@---@
-@-----@
Please explain how to do??请解释如何做??
There are a few things missing and to be improved in your code:您的代码中缺少一些需要改进的地方:
"-" * 3 == "---"
"-" * 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 + "@")
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.