如何在同一行上输入两个三角形

How do I Type Two Triangles on the Same Line

我在学校一直在做这个项目,我们需要并排绘制两个菱形,并且其大小由用户输入决定。

我看了这个视频,告诉我如何画一个菱形,它本质上是两个金字塔三角形,一个尖朝上,另一个朝下。

我正在努力解决的问题是如何在已经存在的两个三角形旁边键入另外两组三角形,而不是直接在它们下面。

我看到如何制作第一颗钻石的问题中的视频显示了我想要的东西的图片,称为双山,但它没有详细解释如何制作。

我是编码新手,我在这个网站上看到类似的问题得到了解答,但这些答案看起来不像我想做的,所以如果有人能花点时间帮助我解决这个问题,那就太棒了。

我的代码

diamond = int(input("Diamond size: "))

#Print first diamond
for i in range(diamond):
    for j in range(i, diamond):
     print(" ", end="")
     
    for j in range(i):
     print("*", end="")
     
    for j in range(i + 1):
     print("*", end="")
    
    print()
    
for i in range(diamond):
    for j in range(i + 1):
     print(" ", end="")
     
    for j in range(i, diamond - 1):
     print("*", end="")
     
    for j in range(i, diamond):
     print("*", end="")
    
    print()

我看过的视频 https://www.youtube.com/watch?v=fX64q6sYom0

print() 语句插入一个新行。所以你只需要复制你的 for 语句并将它们粘贴到 print() 语句

之前

这是我编写的初始代码,应该可以解决您的问题:

diamond = int(input("Diamond size: "))

#Print first diamond
for i in range(diamond):
    for j in range(i, diamond):
     print(" ", end="")
     
    for j in range(i):
     print("*", end="")
     
    for j in range(i + 1):
     print("*", end="")


    # adding the same code here to make the lower triangle
    for j in range(i, diamond):
     print(" ", end="")


    # adding an additional for loop of spaces 
    for j in range(i, diamond):
     print(" ", end="")
     
    for j in range(i):
     print("*", end="")
     
    for j in range(i + 1):
     print("*", end="")
 
    print()
    
for i in range(diamond):
    for j in range(i + 1):
     print(" ", end="")
     
    for j in range(i, diamond - 1):
     print("*", end="")
     
    for j in range(i, diamond):
     print("*", end="")
    
    # adding the same code here to make the lower triangle
    for j in range(i + 1):
     print(" ", end="")
     
    # adding an additional for loop of spaces 
    for j in range(i + 1):
     print(" ", end="")

    for j in range(i, diamond - 1):
     print("*", end="")
     
    for j in range(i, diamond):
     print("*", end="")

    print()

我得到的结果:

Diamond size: 5
     *          *
    ***        ***
   *****      *****
  *******    *******
 *********  *********
 *********  *********
  *******    *******
   *****      *****
    ***        ***
     *          *

您可以使用 print 语句的函数来逃避这些 for 循环:

#Print first diamond
for i in range(diamond):
    print(" "*(diamond-i), end="")
    print("*"*i, end="")
    print("*"*(i+1), end="")

    # adding an additional for loop of spaces 
    print(" "*(diamond-i), end="")


    # adding the same code here to make the lower triangle
    print(" "*(diamond-i), end="")
    print("*"*i, end="")
    print("*"*(i+1), end="")


    print()
    
for i in range(diamond):
    print(" "*(i+1), end="")
    print("*"*(diamond-1-i), end="")
    print("*" * (diamond - i), end="")
    
     
    # adding an additional for loop of spaces 
    print(" "*(i+1), end="")

    # adding the same code here to make the lower triangle
    print(" "*(i+1), end="")
    print("*"*(diamond-1-i), end="")
    print("*" * (diamond - i), end="")

    print()

你应该花一些时间来优化你的代码,但我不知道你是否了解函数。如果这样做,请尝试创建一个用于创建上下三角形的函数

更新:

要在菱形之间只有一个 space,只需将额外间距减去 1

diamond = int(input("Diamond size: "))

#Print first diamond
for i in range(diamond):
    print(" "*(diamond-i), end="")
    print("*"*i, end="")
    print("*"*(i+1), end="")

    # adding an additional for loop of spaces 
    print(" "*(diamond-i-1), end="")


    # adding the same code here to make the lower triangle
    print(" "*(diamond-i), end="")
    print("*"*i, end="")
    print("*"*(i+1), end="")


    print()
    
for i in range(diamond):
    print(" "*(i+1), end="")
    print("*"*(diamond-1-i), end="")
    print("*" * (diamond - i), end="")
    
     
    # adding an additional for loop of spaces 
    print(" "*(i), end="")

    # adding the same code here to make the lower triangle
    print(" "*(i+1), end="")
    print("*"*(diamond-1-i), end="")
    print("*" * (diamond - i), end="")

    print()