我需要打印一个用户输入4个不同参数的树形。枝高,枝宽,茎高和茎宽。我有两个形状分别构成树的顶部和底部,但我似乎无法弄清楚如何将它们放在一起,使其看起来像一棵树。我认为我需要计算分支的宽度并从中减去茎,但我不确定。我的输出当前看起来像这样:
有什么建议吗?
Enter height of the branches: 5
*
***
*****
*******
*********
Enter width of the stem: 5
*****
*****
*****
*****
*****
def pyramid(height):
for row in range(height):
for count in range(height - row):
print(end=" ")
for count in range(2 * row + 1):
print(end="*")
print()
def square(width):
for i in range(width):
for j in range(width):
print('*', end='')
print()
height = int(input("Enter height of the branches: "))
pyramid(height)
width = int(input("Enter width of the stem: "))
square(width)
答案 0 :(得分:2)
可以尝试:
@foreach (var item in Model)
{
<div class="card-body">
<h5 class="card-title text-black-50 font-weight-bold text-center">Complaint Number : @item.ComplaintId</h5>
<hr />
<br />
<hr />
<p class="card-text">Employee Name : @item.Complaint.Employee.Name</p>
<p class="card-text">Phone Number : @item.Complaint.Employee.Phone</p>
<p class="card-text">City: @item.Complaint.Employee.City </p>
<p class="card-text">E-mail: @item.Complaint.Employee.Email </p>
}
<p class="card-text">Dealer Name : @item.Complaint.Dealer.Name</p>
<p class="card-text">Phone Number : @item.Complaint.Dealer.Phone</p>
<p class="card-text">City: @item.Complaint.Dealer.City </p>
<p class="card-text">E-mail: @item.Complaint.Dealer.Email </p>
}
答案 1 :(得分:1)
您可以在词干的每一行之前添加足以填充金字塔高度减去词干宽度一半的空白:
def pyramid(height):
for row in range(height):
for count in range(height - row):
print(end=" ")
for count in range(2 * row + 1):
print(end="*")
print()
def square(width, pyramid_height):
for i in range(width):
print(' ' * (pyramid_height - width // 2), end='')
for j in range(width):
print('*', end='')
print()
height = int(input("Enter height of the branches: "))
pyramid(height)
width = int(input("Enter width of the stem: "))
square(width, height)
答案 2 :(得分:1)
您正在寻找str.center(width\[, fillchar\])
:
def pyramid(height):
for row in range(height):
print(('*' * (2 * row + 1)).center((2 * height + 1)))
def square(width, height):
for i in range(width):
print(('*' * (width)).center((2 * height + 1)))
height = int(input("Enter height of the branches: "))
pyramid(height)
width = int(input("Enter width of the stem: "))
square(width, height)
出局:
C:\_\Python363-64\python.exe C:/Users/MrD/.PyCharm2018.2/config/scratches/scratch_75.py
Enter height of the branches: 5
*
***
*****
*******
*********
Enter width of the stem: 5
*****
*****
*****
*****
*****
Process finished with exit code 0