2019-05-16 23:10

# Python3 打印三角形-- 三角形右边多出多余的空格

5

``````import sys
siz = input('Enter height: ')
if (not str(siz).isdigit()) or int(siz) >20 or int(siz)<1:
print()
print('Invalid height.')
exit()
lev = input('Enter number of rows: ')
if (not str(lev).isdigit()) or int(lev) >20 or int(lev)<1:
print()
print('Invalid number of rows.')
exit()
print()
height=int(siz)
row=int(lev)
def solve(height, row):
for i in range(0, row):
for k in range(0, height):
for j in range(0, (row - i) * height - k-1):
print(" ",end='')
if (k != height - 1):
for j in range(0, i+1):
print("/",end='')
for l in range(0, k * 2):
print(" ",end='')
print("\\",end='')
for l in range(0, (height - k - 1) * 2):
print(" ",end='')
else:
for j in range(0, i + 1):
print("/",end='')
for l in range(0, (height - 1) * 2):
print("_",end='')
print("\\",end='')
print("\n",end='')
solve(height, row)
``````

• 点赞
• 写回答
• 关注问题
• 收藏
• 复制链接分享
• 邀请回答

#### 1条回答

• 回答这么多问题就耍赖把我的积分一笔勾销了 2年前

不好意思，才看到。下次提问不要悬赏

``````# encoding: utf-8

import sys

def solve(size, level):
for i in range(0, level):
for k in range(0, size):
for j in range(0, (level - i) * size - k):
sys.stdout.write(" ")
if (k != size - 1):
for j in range(0, i + 1):
sys.stdout.write("/")
for l in range(0, k * 2):
sys.stdout.write(" ")
sys.stdout.write("\\")
if (j != i): 这里加上
for l in range(0, (size - k - 1) * 2):
sys.stdout.write(" ")
else:
for j in range(0, i + 1):
sys.stdout.write("/")
for l in range(0, (size - 1) * 2):
sys.stdout.write("_")
sys.stdout.write("\\")
sys.stdout.write("\n")

solve(3, 4)
``````
点赞 1 评论 复制链接分享