My Approach Code:
n=int(input(":"))
for i in range(n+1):
for j in range(1,i+1):
print(","+str(j), end="")
print()
for i in range(n, 1, -1):
for j in range(1, i):
print(","+str(j), end="")
print()
But Couldnt get required pattern pls help
You can use "," as the print separator instead of concatenating the comma as long as you print all numbers in one call to print (which you can do with unpacking):
n = 5
for d in range(1-n,n):
print(*range(1,n-abs(d)+1),sep=",")
1
1,2
1,2,3
1,2,3,4
1,2,3,4,5
1,2,3,4
1,2,3
1,2
1
If you want to adjust your code, you could use a variable for the comma and only have it contain an actual comma from the second number onward:
n=int(input(":"))
for i in range(n+1):
comma = ""
for j in range(1,i+1):
print(comma+str(j), end="")
comma = ","
print()
for i in range(n, 1, -1):
comma = ""
for j in range(1, i):
print(comma+str(j), end="")
comma = ","
print()
Note that you are printing an extra blank line at the beginning. If that is not your intention, you should make the first loop use range(1,n+1)
Another way to fix your code would be to systematically print a comma as the ending character and use the final print for the last number on each line:
n=int(input(":"))
for i in range(1,n+1):
for j in range(1,i):
print(j, end=",")
print(i)
for i in range(n-1, 0, -1):
for j in range(1, i):
print(j, end=",")
print(i)
I follow your code and get this result. What point do you want to change?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.