Python 打印金字塔技术的程序

您所在的位置:网站首页 python输出aaba九层正金字塔 Python 打印金字塔技术的程序

Python 打印金字塔技术的程序

2024-05-19 05:08| 来源: 网络整理| 查看: 265

Python 打印金字塔技术的程序

Python提供了用于打印图案的基本for循环。第一个外层循环管理行的数量,而内部嵌套循环管理列的数量。通过修改打印语句,可以打印新的数字图案、单词图案和星星图案。

本文说明了其中的几个图案。

1.简单的金字塔模式 # Python 3.x code for star pattern demonstration Printing pattern demonstration #function def pypart(a): # outer loop in this case to manage the number of rows for i in range(0, a): # Inner loop to manage values changing according to the outer loop's number # of columns. for j in range(0, i+1): # printing up stars print("* ",end="") # final line following each row print("\r") # Driver Code a = 5 pypart(a)

输出

* * * * * * * * * * * * * * * 2.Python 3的List功能会使这个过程更简单 # Star pattern demonstration in Python 3.x # Displays a printing pattern in action def pypart(a): myList1 = [] for i in range(1, a+1): myList.append("*"*i) print("\n".join(myList1)) # Driver Code a = 5 pypart(a)

输出

* ** *** **** ***** 3.递归法 # Pyramid pattern printing in Python 3 using recursion def pypart(a): if a == 0: return else: pypart(a-1) print("* "*a) # Driver Code n = 5 pypart(n)

输出

* * * * * * * * * * * * * * * 4.使用While Loop # Pyramid pattern printing in Python 3 with while loop a=5 i=1; j=0 # Until the condition becomes false in the while loop, the condition is checked. # If it is true, enter the loop & print the pattern. while(i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3