Python prints various triangles

# Print the lower left triangle: for i in range(10): After that, range(0,i) 
# Print the upper right triangle: On the basis of the lower left corner, change "-" into " "space
copy code
for i in range(10):
    for j in range(0,i):
        print("-",end=" ")

    for j in range(i,10):
        print("$", end=" ")

    print("")

print("-------------------------")
copy code

 



# Print the upper left triangle: for i in range(10): After that, range(0,10-i)
# Print the lower right triangle: On the basis of the upper left corner, change "-" into " "space
copy code
for i in range(10):
    for j in range(0, 10 - i):
        print("-", end=" ")
    for k in range(10 - i, 10):
        print("$", end=" ")

    print("")
copy code

 



# To print the upper triangle, just remove the "-"
copy code
for i in range(10):
    for j in range(0, 10 - i):
        print(end=" ")
    for k in range(10 - i, 10):
        print("$", end=" ")

    print("")
copy code

 




# To print an inverted triangle, just remove the "-"
copy code
for i in range(10):
    for j in range(0,i):
        print(end=" ")

    for j in range(i,10):
        print("$", end=" ")

    print("")
copy code

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324822552&siteId=291194637