python: 打印星星塔
# print star_tower func;
def star_tower(char="*", num=7):
"""
Based on observations of the model, n = floors:
------------* # 12 - [2*n-2*i]=12 and 1 * [2*i-1]=1
----------* * * # 10 - [2*n-2*i]=10 and 3 * [2*i-1]=3
--------* * * * * # 8 - [2*n-2*i]= 8 and 5 * [2*i-1]=5
------* * * * * * * # 6 - [2*n-2*i]= 6 and 7 * [2*i-1]=7
----* * * * * * * * * # 4 - [2*n-2*i]= 4 and 9 * [2*i-1]=9
--* * * * * * * * * * * # 2 - [2*n-2*i]= 2 and 11 * [2*i-1]=11
* * * * * * * * * * * * * # 0 - [2*n-2*i]= 0 and 13 * [2*i-1]=13
1. To print a 7-story tower, loop '7' times to print '7' lines of data;
2. Each row consists of 'Spaces' and '*';
3. According to observation, find the relationship between the number of 'Spaces' and the number of' floors';
4. According to observation , find the relationship between the number of 'Spaces' and the number of' *';
:param char: Provides the characters that make up the tower
:param num: Provides the floors that make up the tower
:return: None
"""
n = int(num) # cast to int?;
for i in range(1, n + 1): # according to number of the floors to loop;
for k in range(2 * n - 2 * i): # loop to print 'Spaces', the number is:[2*n-2*i];
print("", end=" ") # print 'Spaces',no \n;
for j in range(2 * i - 1): # loop to print '*', the number is:[2*i-1];
print(char, end=" ")
print() # out \n;
if __name__ == '__main__':
star_tower("Y", '12')
star_tower()
本文来自博客园,作者:Annzi-Py,转载请注明原文链接:https://www.cnblogs.com/annzi/p/15427267.html

浙公网安备 33010602011771号