1. Print solid rhombus
Hello! This is the welcome page that will be displayed the first time you use the Markdown editor. If you want to learn how to use the Markdown editor, you can read this article carefully to learn the basic syntax of Markdown.
Method I:
a = int(input("Enter the number of stars on each side of the rhombus:")) b = a c = a for i in range(1, a + 1): # Print the square triangle first, made up of spaces and * according to the pattern print(" " * (b - 1), "*" * (2 * i - 1)) b -= 1 if i == a: # Critical point, when printing here, start printing inverted triangles for y in range(1, a): print(" " * y, "*" * (2*c-3)) c -= 1
Method II:
n = int(input("Enter the number of elements per side of the rhombus to be printed:")) list_a = [i for i in range(n)] # Generate a list of the first n rows of lines, e.g. [0,1,2,3,4]. list_b = list_a[0:len(list_a) - 1:] # Generate a list of remaining rows and reverse it, e.g. [0,1,2,3] list_c = list_b[::-1] # List and reverse the remaining lines for easy printing operations list_d = list_a + list_c # Merge the two lists print(list_d) b = [' ' * (n - i) + '*' * (2 * i + 1) for i in list_d] # Print the spaces " " and "*" according to the pattern. for line in b: print(line)
Print results:
2. Print hollow rhombus
Code:
a = int(input("Enter the number of stars on each side of the rhombus:")) b = a c = a print(" " * (a - 1), "*") for i in range(2, a+1): # Print the square triangle first, made up of spaces and * according to the pattern print(" " * (b - 1) + "*" + " " * (2 * i - 3) + "*") b -= 1 if i == a: # Critical point, when printing here, start printing inverted triangles for y in range(2, a): print(" " * y+"*"+" "*(2*c-5)+ "*" ) c -= 1 print(" "*a+"*")
Print results:
This Python implementation of printing solid and hollow rhombus above is all I have to share with you, I hope it can give you a reference, and I hope you support me more.