Python练习实例023
问题:打印出如下图案(菱形)
* *** ***** ******* ***** *** *
#! /usr/bin/env python3 # -*- coding:utf-8 -*- # Author : Ma Yi # Blog : http://www.cnblogs.com/mayi0312/ # Date : 2020-06-19 # Name : demo023 # Software : PyCharm # Note : 打印出如下图案(菱形) # * # *** # ***** # ******* # ***** # *** # * def print_lozenge(num): """ 打印菱形 :param num: 菱形的行数(奇数) :return: """ # 打印菱形的上半部分 for i in range(num // 2 + 1): print(" " * (num // 2 - i) + "*" * (2 * i + 1)) # 打印菱形的下半部分 for j in range(num // 2): print(" " * (j + 1) + "*" * (num - 2 * (j + 1))) # 入口函数 if __name__ == '__main__': print_lozenge(7)
运行结果:
* *** ***** ******* ***** *** *