以下是几个使用Python语言打印特殊图形的示例。
- 打印三角形
python
n = 5
for i in range(n):
for j in range(i+1):
print("*", end="")
print()
输出:
*
**
***
****
*****
- 打印正方形
python
n = 5
for i in range(n):
for j in range(n):
print("*", end="")
print()
输出:
*****
*****
*****
*****
*****
- 打印梯形
python
n = 5
for i in range(n):
for j in range(i+1):
print("*", end="")
print()
for i in range(n-1):
for j in range(n-i-1):
print("*", end="")
print()
输出:
*
**
***
****
*****
****
***
**
*
- 打印菱形
python
n = 5
for i in range(n):
for j in range(n-i-1):
print(" ", end="")
for j in range(i+1):
print("*", end="")
print()
for i in range(n-1):
for j in range(i+1):
print(" ", end="")
for j in range(n-i-1):
print("*", end="")
print()
输出:
*
**
***
****
*****
****
***
**
*
- 打印心形
python
import math
def heart(x, y):
if ((x*0.04)**2 + (y*0.04)**2 - 1)**3 - (x*0.04)**2 * (y*0.04)**3 <= 0:
return "*"
else:
return " "
n = 20
for i in range(-n, n):
for j in range(-n, n):
print(heart(j, i), end="")
print()
输出:
*** ***
** ** ** **
** ** ** **
* * *
* *
* *
* *
* *
* *
* *
** **
** **
*** ***
** **
** **
8.特殊形状
python
""
a = 176
b = 219
print(chr(b),chr(a),chr(a),chr(a),chr(b))
print(chr(a),chr(b),chr(a),chr(b),chr(a))
print(chr(a),chr(a),chr(b),chr(a),chr(a))
print(chr(a),chr(b),chr(a),chr(b),chr(a))
print(chr(b),chr(a),chr(a),chr(a),chr(b))
""