Python 3.X 练习集100题 05

用 *号输出字母 C的图案 

方法1:

print("    *****   ")
print("   **    *  ")
print("  **       ")
print("  **       ")
print("   **    *  ")
print("    *****   ")

方法2:

ch = [[0, 1, 1, 0],
      [1, 0, 0, 1],
      [1, 0, 0, 0],
      [1, 0, 0, 0],
      [1, 0, 0, 1],
      [0, 1, 1, 0]]

for i in range(len(ch)):
    for j in ch[i]:
        if j == 0:
            print(" ",end="")
        else:
            print("*", end=" ")

    print()

 

输出结果:

Python 3.X 练习集100题 05_第1张图片

 

方法3:

from PIL import Image,ImageDraw,ImageFont
import numpy as np

text = "C"
myfont = ImageFont.truetype("msyh.ttc", 12)  # 在代码所在目录下需要放置字体文件,此处为msyh.tcc
size = myfont.getsize(text)
img = Image.new("1", size, "black")
draw = ImageDraw.Draw(img)
draw.text((0,0), text, "white", font=myfont)
pixels = np.array(img, dtype=np.uint8)
chars = np.array([' ', '*'], dtype="U1")[pixels]
strings = chars.view('U' + str(chars.shape[1])).flatten()
print("\n".join(strings))

输出结果:

Python 3.X 练习集100题 05_第2张图片

 

转载于:https://www.cnblogs.com/ElegantSmile/p/10830212.html

你可能感兴趣的:(Python 3.X 练习集100题 05)