以下是几个使用Python语言打印特殊图形的示例。
- 打印三角形
n = 5
for i in range(n):
for j in range(i+1):
print("*", end="")
print()
输出:
*
**
***
****
*****
- 打印正方形
n = 5
for i in range(n):
for j in range(n):
print("*", end="")
print()
输出:
*****
*****
*****
*****
*****
- 打印梯形
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()
输出:
*
**
***
****
*****
****
***
**
*
- 打印菱形
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()
输出:
*
**
***
****
*****
****
***
**
*
- 打印心形
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()
输出:文章来源:https://www.toymoban.com/news/detail-707415.html
*** ***
** ** ** **
** ** ** **
* * *
* *
* *
* *
* *
* *
* *
** **
** **
*** ***
** **
** **
8.特殊形状文章来源地址https://www.toymoban.com/news/detail-707415.html
""
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))
""
到了这里,关于python经典百题之特殊图形打印的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!