# 用python实现打印各种图形总结(包括等腰三角形,菱形,近似圆等)
注:打印图形真的太考验我了,我真的一点思路都没有,泪目了。努力看懂每一行代码ing
等腰三角形
# 等腰三角形
lines = int(input("请输入行数:"))
for i in range(lines):
# 行数
for j in range(lines - i):
print(end=" ")
# 一行多少个
for k in range(i * 2 + 1):
print("*", end="")
print()
结果:
请输入行数:5
*
***
*****
*******
*********
菱形
# 菱形
lines = int(input("输入要打印的行数(奇数):"))
if lines % 2 == 0:
print('请输入奇数')
import sys
sys.exit(0)
half_lines = lines // 2 + 1
# 打印上半
for i in range(half_lines):
print(" " * (half_lines - i), end="")
print("*" * (2 * i + 1))
# 打印下半
for i in range(half_lines - 1):
print(" " * (i + 2), end="")
print("*" * (lines - 2 - 2 * i))
结果:
输入要打印的行数(奇数):7
*
***
*****
*******
*****
***
*
空心菱形:
# 空心菱形
lines = int(input("输入要打印的行数(奇数):"))
if lines % 2 == 0:
print('请输入奇数')
import sys
sys.exit(0)
half_lines = lines // 2 + 1
# 打印上半
for i in range(half_lines):
print(" " * (half_lines - i), end="")
if i == 0:
print("*")
else:
print("*", end="")
print(" " * (2 * i - 1), end="")
print("*")
# 打印下半
for i in range(half_lines - 1):
print(" " * (i + 2), end="")
if i == half_lines - 2:
print("*")
else:
print("*", end="")
print(" " * (lines - 4 - 2 * i), end="")
print("*")
结果:
输入要打印的行数(奇数):7
*
* *
* *
* *
* *
* *
*
近似圆
# 近似圆
radius = int(input("请输入半径: "))
for i in range(2 * radius + 1):
half = round((radius ** 2 - (radius - i) ** 2) ** 0.5)
print(" " * (radius - half), end="")
print("*", end="")
print(" " * half * 2, end="")
print("*")
结果:
请输入半径: 3
**
* *
* *
* *
* *
* *
**