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<=a):
while(j<=i-1):
print("* ",end="")
j+=1
# printing the subsequent row's line
print("\r")
j=0;i+=1
输出
*
* *
* * *
* * * *
* * * * *
5.180度的旋转法
# Star pattern demonstration in Python 3.x # Displays a printing pattern in action
def pypart2(a):
# number of spaces
s = 2*s - 2
# row count is handled by an outer loop.
for i in range(0, a):
# inner loop for handling number spaces and changing values in accordance with # requirements
for j in range(0, s):
print(end=" ")
# decrementing s after each loop
s = s - 2
# Inner loop to handle the amount of columns and any values that change
# according to the outer loop.
for n in range(0, m+1):
# printing up stars
print("* ", end="")
# ending up the line after each row
print("\r")
# Driver Code
a = 5
pypart2(a)
输出
*
* *
* * *
* * * *
* * * * *
6.三角形印刷
# Star pattern demonstration in Python 3.x
# Triangle serving as an example of a printing pattern
def triangle(s):
# Number of spaces to be noted
a = b - 1
# Outer loop to handle number of rows
for m in range (0, b):
# inner loop to handle number spaces
# values changing acc. to requirement
for n in range(0, a):
print(end=" ")
# decrementing a after each loop
a = a - 1
# To handle the number of columns and the changing values according to the
# outer loop, use an inner loop.
for n in range(0, m+1):
# printing up the stars
print("* ", end="")
# ending the line after each the row
print("\r")
# Driver Code
b = 5
triangle(b)
输出
*
* *
* * *
* * * *
* * * * *
7.数字模式
# Star pattern demonstration in Python 3.x Function to display the number
def numpat(s):
# Initialising of starting number
num = 1
# outer loop to be handle number of rows
for m in range(0, n):
# re assigning num1
Num1 = 1
# inner loop to be handled at number of columns
# values changing to acc. to outer loop
for n in range(0, m+1):
# printing up number
print(num1, end=" ")
# incrementing the number at each column
Num1 = num1 + 1
# ending up line after each row
print("\r")
# Driver code
s = 5
numpat(s)
输出
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5