一、打印九九乘法表:
#coding:utf-8 for i in range(1,10): for j in range(1,i+1): print("%dx%d=%d") %(j,i,j*i), print ‘\n‘
结果:
二、打印正方形
实体正方形 代码:
#coding:utf-8 rows=int(raw_input("输入正方形边长:")) for i in range(rows): for j in range(rows): print("*"), print
空心正方形 代码:
#coding:utf-8 rows=int(raw_input("输入正方形边长:")) for x in range(rows): for y in range(rows): if x !=0 and x != rows-1: if y==0 or y == rows-1: print(" * "), else: print(" "), else: print(" * "), print("\n")
效果:
三、打印圣诞树
代码:
#coding:utf-8 rows=3 for x in range(rows): for i in range(1,13): if i % 2 ==0: continue else: print(str(i * ‘*‘).center(11)) for y in range(10): print(‘*‘.center(11))
效果:
四、打印凌形
代码:
#coding:utf-8 rows=10 #while True: for i in range(1, rows): if i % 2 == 0: continue else: print(str(i * ‘*‘).center(rows)) for i in range(rows): if i % 2 == 0: continue else: print(str((rows - i) * ‘*‘).center(rows))
效果:
写简单,如有更好的,请指正并交流~
时间: 2024-10-12 09:16:26