编写一个 Python 程序,使用 For 循环和 While 循环打印指数递增的星形图案,并附带示例。
Python 使用 While 循环打印指数递增星形图案的程序
此 Python 程序允许用户输入总行数。接下来,我们使用 Python 嵌套 While 循环打印从 1 到用户指定的最大值(行数)的指数递增的星形。
# Python Program to Print Exponentially Increasing Star Pattern
import math
rows = int(input("Please Enter the total Number of Rows : "))
print("Exponentially Increasing Stars")
i = 0
while(i <= rows):
j = 1
while(j <= math.pow(2, i)):
print('*', end = ' ')
j = j + 1
i = i + 1
print()

Python 使用 For 循环打印指数递增星形的程序
此指数递增星形图案程序与第一个示例相同。但是,我们将 While 循环 替换为 For 循环。
# Python Program to Print Exponentially Increasing Star Pattern
import math
rows = int(input("Please Enter the total Number of Rows : "))
print("Exponentially Increasing Stars")
for i in range(rows + 1):
for j in range(1, int(math.pow(2, i) + 1)):
print('*', end = ' ')
print()
Please Enter the total Number of Rows : 3
Exponentially Increasing Stars
*
* *
* * * *
* * * * * * * *
>>>
Python 打印指数递增星形的示例 2
此 Python 程序 允许用户输入他的/她的字符。接下来,Python 打印用户指定的字符的指数递增图案。
# Python Program to Print Exponentially Increasing Star Pattern
import math
rows = int(input("Please Enter the total Number of Rows : "))
ch = input("Please Enter any Character : ")
print("Exponentially Increasing Stars")
for i in range(rows + 1):
for j in range(1, int(math.pow(2, i) + 1)):
print('%c' %ch, end = ' ')
print()
Please Enter the total Number of Rows : 4
Please Enter any Character : #
Exponentially Increasing Stars
#
# #
# # # #
# # # # # # # #
# # # # # # # # # # # # # # # #
>>>