Python编程输出数字图形(编写python程序,输出*图形)

http://www.itjxue.com  2023-03-27 12:13  来源:未知  点击次数: 

如何用python编程输出如下图形 1 121 12321?

层数 = 3

for i ?in range(1,层数+1):

print(" "*(层数-i),end="") ? ? ? ?# 数字左侧的空格

for j in range(1,i+1): ? ? ? ? ? ?# 显示左侧数字

print(j,end="")

for j in range(i-1,0,-1): ? ? ? ? # 显示右侧数字

print(j,end="")

print(" " * (层数 - i), end="") ? # 数字右侧的空格

print() ? ? ? ? ? ? ? ? ? ? ? ? ? # 换行

请教如图所示Python输出数字菱形的代码?

按照你的要求编写的输出数字菱形的Python程序如下(abs是求绝对值的函数)

N=9

for i in range(1,N*2):

for j in range(1,abs(N-i)+1):

print(' ',end='')

for j in range(N-abs(N-i),1,-1):

print(j,end='')

for j in range(1,N-abs(N-i)+1):

print(j,end='')

print()

源代码(注意源代码的缩进)

python编程,输入一个字符,用该字符输出一下图形,是一个数字6,用*表示6

#?encoding:utf-8

#?python3.6

#?定义数字对应的字符字典

c_number?=?{0:?[[1,?1,?1],

????????????????[1,?0,?1],

????????????????[1,?0,?1],

????????????????[1,?0,?1],

????????????????[1,?1,?1]],

????????????1:?[[0,?1,?0],

????????????????[0,?1,?0],

????????????????[0,?1,?0],

????????????????[0,?1,?0],

????????????????[0,?1,?0]],

????????????2:?[[1,?1,?1],

????????????????[0,?0,?1],

????????????????[1,?1,?1],

????????????????[1,?0,?0],

????????????????[1,?1,?1]],

????????????3:?[[1,?1,?1],

????????????????[0,?0,?1],

????????????????[1,?1,?1],

????????????????[0,?0,?1],

????????????????[1,?1,?1]],

????????????4:?[[1,?0,?1],

????????????????[1,?0,?1],

????????????????[1,?1,?1],

????????????????[0,?0,?1],

????????????????[0,?0,?1]],

????????????5:?[[1,?1,?1],

????????????????[1,?0,?0],

????????????????[1,?1,?1],

????????????????[0,?0,?1],

????????????????[1,?1,?1]],

????????????6:?[[1,?1,?1],

????????????????[1,?0,?0],

????????????????[1,?1,?1],

????????????????[1,?0,?1],

????????????????[1,?1,?1]],

????????????7:?[[1,?1,?1],

????????????????[0,?0,?1],

????????????????[0,?1,?0],

????????????????[0,?1,?0],

????????????????[0,?1,?0]],

????????????8:?[[1,?1,?1],

????????????????[1,?0,?1],

????????????????[1,?1,?1],

????????????????[1,?0,?1],

????????????????[1,?1,?1]],

????????????9:?[[1,?1,?1],

????????????????[1,?0,?1],

????????????????[1,?1,?1],

????????????????[0,?0,?1],

????????????????[1,?1,?1]],

????????????}

#?定义打印字符函数

def?print_one_number(x):

????number?=?c_number[int(x)]

????for?i?in?range(len(number)):

????????for?j?in?range(len(number[0])):

????????????if?number[i][j]?==?1:

????????????????print("*",?end="")

????????????else:

????????????????print("?",?end="")

????????print("")

if?__name__?==?"__main__":

????print("Input?a?number?between?0?and?9:")

????n?=?input()

????print_one_number(n)

运行上面的程序,输入0到9任意一个数字,回车的后在命令行打印出对应图形。

下面是运行此脚本后,输入数字6,回车后的结果:

(责任编辑:IT教学网)

更多

推荐微信营销文章