gpt4 book ai didi

python - 我希望所有三个形状都在同一条线上......请帮忙......!

转载 作者:太空宇宙 更新时间:2023-11-04 01:43:25 24 4
gpt4 key购买 nike

#Top half of triangle    
for rows in range (5):
for row in range (12):
print("-", end='')
print()


for row in range (5):
stars=0
while stars<=row:
print("*", end='')
stars=stars+1
print()


for row in range(5):
star=4
while star>=row:
print("*", end='')
star=star-1
print()

最佳答案

shape1 = [12*'-' for i in range(5)]                  # segments of rectangle
shape2 = [i*'*' + (5-i)*' ' for i in range(1,5+1)] # segments of 1st triangle
shape3 = [(5-i)*' ' + i*'*' for i in range(1,5+1)] # segments of 2nd triangle

for line in zip(shape1, shape2, shape3):
print(" ".join(line))

编辑:详细版本,按要求(但我这里没有 python 3;以下代码在 python 2.x 中工作,所以你必须稍微修改一下打印指令):

for line in range(1, 5+1):        # for each line
for c in range (12): # print a bit of the first shape
print '-',
print " ",

for c in range (line) : # a bit of the second
print '*',
for c in range (5-line):
print ' ',
print " ",

for c in range (5+1-line): # and a bit of the third
print '*',
#for c in range (line):
# print ' ',
print

关于python - 我希望所有三个形状都在同一条线上......请帮忙......!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2564972/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com