目录
要求:按照其不同的形式(格式)在控制台中输出。
直角三角形(奇数增加)
*
***
*****
*******
*********
***********
代码如下:
#while循环实现:
layer = int(input("请输入你要输入的行数:"))
index = 1
while index <= layer:
j = 1
while j<= (index * 2 - 1):
print("*", end = "")
j += 1
print()
index += 1
#while循环简易版:
#layer = int(input("请输入你要输入的行数:"))
#index = 1
#while index <= layer:
# print("*"*(2*index-1))
# index += 1
#for循环实现:
#layer = int(input("请输入你要输入的行数:"))
#for i in range(1,layer + 1 ):
# for j in range(i * 2 - 1):
# print("*", end = "")
# print()
#for循环简易版:
# layer = int(input("请输入你要输入的行数:"))
#for i in range(layer + 1):
# print('*'*(2*i-1))
直角三角形
*
**
***
****
*****
******
代码如下:
layer = int(input("请输入你要输入的行数:"))
index = 1
while index <= layer:
j = 1
while j <= index:
print("*", end = "")
j += 1
print()
index += 1
#while循环简易版:
# layer = int(input("请输入你要输入的行数:"))
# index = 1
# while index <= layer:
# print("*" * index)
# index += 1
#使用for循环:
# layer = int(input("请输入你要输入的行数:"))
# for i in range(1,layer + 1):
# for j in range(i):
# print("*", end = " ")
# print()
#
#for循环简易版:
# layer = int(input("请输入你要输入的行数:"))
# for i in range(1,layer + 1):
# print("*"*i)
反转的直角三角形
*
**
***
****
*****
******
代码如下:
#while循环:
layer = int(input("请输入你要输入的行数:"))
index = 1
while index <= layer:
print(" " * (layer - index) + "*" * index )
index += 1
#for嵌套循环:
#layer = int(input("请输入你要输入的行数:"))
#for i in range(1,layer + 1 ):
# for j in range(layer - i):
# print(" ", end = "")
# for k in range(i):
# print("*", end = "")
# print()
#for循环:
#layer = int(input("请输入你要输入的行数:"))
#for i in range(1,layer + 1 ):
# print(" " * (layer - i) + "*" * i)
等腰三角形
*
***
*****
*******
*********
***********
代码如下:
layer = int(input("请输入你要输入的行数:"))
for i in range(1, layer+1):
space_num = layer - i
for j in range(0, space_num):
print(" ", end = "")
star_num = 2 * i - 1
for j in range(0, star_num):
print("*", end ="" )
print()
#for循环简易版:
#layer = int(input("请输入你要输入的行数:"))
#for i in range(1, layer+1):
# print(" " * (layer - i),end = "")
# print("*" * (2 * i - 1))
倒三角形
***********
*********
*******
*****
***
*
代码如下:
layer = int(input("请输入你要输入的行数:"))
for i in range(layer, 0, -1):
space_num = layer - i
for j in range(0, space_num):
print(" ", end="")
star_num = 2 * i - 1
for j in range(0, star_num):
print("*", end="")
print()
#for循环简易版:
# layer = int(input("请输入你要输入的行数:"))
# for i in range(layer, 0 , -1):
# print(" " * (layer - i),end = "")
# print("*" * (2 * i - 1))
菱形
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
代码如下:
layer = int(input("请输入你要输入的行数:"))
while layer % 2 == 0:
layer = int(input("请输入奇数行:"))
for i in range(1 , (layer // 2 + 2)):
for j in range(layer - i):
print(" ", end = "")
for j in range(2 * i - 1):
print("*", end = "")
print()
for i in range(layer // 2, 0, -1):
print(" ", end = "")
for j in range(layer - i):
print(" ", end = "")
for j in range(2 * i - 1):
print("*", end = "")
print()
空心菱形
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
layer = int(input("请输入你要输入的行数:"))
while layer % 2 == 0:
layer = int(input("请输入奇数行:"))
for i in range(1 , (layer // 2 + 2)):
for j in range(layer - i):
print(" ", end = "")
for j in range(2 * i - 1):
if j == 0 or j == 2 * i - 2:
print("*", end = "")
else:
print(" ", end = "")
print()
for i in range(layer // 2, 0, -1):
for j in range(layer - i):
print(" ", end = "")
for j in range(2 * i - 1):
if j == 0 or j == 2 * i - 2:
print("*", end = "")
else:
print(" ", end = "")
print()
对角线菱形
*
***
* * *
* * *
* * *
***********
* * *
* * *
* * *
***
*
代码如下:
layer = int(input("请输入你要输入的行数:"))
while layer % 2 == 0:
layer = int(input("请输入奇数行:"))
for i in range(1 , (layer // 2 + 2)):
for j in range(layer - i):
print(" ", end = "")
for j in range(2 * i - 1):
if j == 0 or j == 2 * i - 2 or j == i - 1 or i == layer // 2 + 1:
print("*", end = "")
else:
print(" ", end = "")
print()
for i in range(layer // 2, 0, -1):
for j in range(layer - i):
print(" ", end = "")
for j in range(2 * i - 1):
if j == 0 or j == 2 * i - 2 or j == i - 1:
print("*", end = "")
else:
print(" ", end = "")
print()