2019-05-15 23:42

# Python3: 根据给定的高度和行数来打印三角形(Nforce)

40

(Python3)Triforce题目要求您打印给定大小的三个小三角形。 在此题目(Nforce)中，需要打印任意数量的给定大小的三角形。

Triforce题目的代码和输出结果：

``````import sys
zyz_k = input('输入高度: ')
z = int(zyz_k)
print()
def zyz(a):
if a==z-1:
sys.stdout.write('/'+'_'*(2*z-2)+'\\')
else:
sys.stdout.write(' '*(z-a-1)+'/'+' '*2*a+'\\')
if(not zyz_k.isdigit()):
print('Invalid height.')
elif z > 20 or z < 2:
print('Invalid height.')
else:
for l in range(2*z):
if l<int(zyz_k):
sys.stdout.write(' '*z)
zyz(l)
print()
else:
g=l-z
zyz(g)
sys.stdout.write(' '*(z-g-1))
zyz(g)
print()
``````

1:

``````        输入高度: 5
``````

2:

``````        输入高度: 64

Invalid height.
``````

3:

``````        输入高度: -64

Invalid height.
``````

4:

``````        输入高度: zelda

Invalid height.
``````

1

``````输入高度(Enter height): 1

/\
``````

2

3

``````输入高度(Enter height): 5

Invalid number of rows.
``````

4

``````输入高度(Enter height): 5

Invalid number of rows.
``````

5

``````输入高度(Enter height): 5

Invalid number of rows.
``````

6

``````输入高度(Enter height): ok

Invalid height.
``````

7

``````输入高度(Enter height): 0

Invalid height.
``````

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

#### 1条回答

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

https://ask.csdn.net/questions/703885

``````# 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("\\")
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)
``````

点赞 评论 复制链接分享