python 使用while循環(huán)輸出*組成的菱形實例
更新時間:2020年04月12日 10:47:44 作者:百奎先生
這篇文章主要介紹了python 使用while循環(huán)輸出*組成的菱形實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
一,python輸出*組成的菱形(實心):
python代碼:
x = int(input('請輸入最長行的*的個數(shù):'))
y = int(input('請輸入每個*之間的間隔:'))
i = 1
while i<= x: #第一個while組成菱形上半部
j = 1
k = 1
while k <= x-i:
print(' '*y,end = '') #每一行最左邊到第一個*之間的空格
k += 1
while j <= i:
print('*', end=' '*(2*y-1)) #每一行*的個數(shù)和*與*之間的間隔,要計算好才能上下對齊
j += 1
print('\n')
i += 1
while i <= 2*x-1: #第二個while組成菱形下半部
a = x+1
b = 2*x-1
while a <= i:
print(' '*y, end='')
a += 1
while b >= i:
print('*', end=' '*(2*y-1))
b -= 1
print('\n')
i += 1

二,python輸出*組成的菱形(空心):
代碼
x = int(input('請輸入最長行的*的個數(shù):'))
y = int(input('請輸入每個*之間的間隔:'))
i = 1
#前兩個while組成上半部
while i <= 1: #菱形上頂點單獨列出
k = 1 #寫出最左邊到*的空格和第一個*
while k <= x - i:
print(' ' * y, end='')
k += 1
print('*')
print('\n')
i += 1
while i<= x:
k = 1
j = 1
while k <= x-i: #這個while寫出最左邊到左邊*的空格數(shù)
print(' '*y,end = '')
k += 1
print('*', end=' '*(2*y-1)) #這寫出左邊*,*相當于占一個空格,所以*后面的空格少一個,才能上下對齊
while j <= i-2: #這個while寫出左邊*到右邊*的空格數(shù)
print(' '*2*y,end='')
j += 1
print('*') #這寫出右邊*
print('\n')
i += 1
#后兩個while組成下半部
while i <= 2*x-2:
k = x+1
j = 2*x-2
while k <= i:
print(' '*y, end='')
k += 1
print('*', end=' '*(2*y-1))
while j-1 >= i:
print(' ' *2* y, end='')
j -= 1
print('*',end='')
print('\n')
i += 1
while i <= 2*x-1: #菱形下頂點單獨列出
k = 2 * x - 1
while k >= i+2 - x:
print(' '*y, end='')
k -= 1
print('*')
print('\n')
i += 1

補充知識:Python -- 使用循環(huán)語句永 “ * ” 輸出一個對線菱形
#使用循環(huán)語句輸出一個對線菱形
layer = int(input("請輸入要打印的層數(shù):"))
#保證輸入的數(shù)字是奇數(shù)
while layer % 2 == 0:
#偶數(shù)
layer = int(input("輸入層數(shù),必須是一個奇數(shù):"))
#上半部分
for x in range(1, layer // 2 + 2):
# 先打印空格
space_num = layer - x
for y in range(1, space_num + 1):
print(" ", end="")
# 在打印*
star_num = 2 * x - 1
for y in range(1, star_num + 1):
if y == 1 or y == star_num or x == layer // 2 + 1 or y == star_num // 2 + 1:
print("*", end="")
else:
print(" ", end="")
# 換行
print("")
#下半部分
for x in range(layer // 2, 0, -1):
# 先打印空格
space_num = layer - x
for y in range(1, space_num + 1):
print(" ", end="")
# 在打印*
star_num = 2 * x - 1
for y in range(1, star_num + 1):
if y == 1 or y == star_num or x == layer // 2 + 1 or y == star_num // 2 + 1:
print("*", end="")
else:
print(" ", end="")
# 換行
print("")
輸出舉例:

以上這篇python 使用while循環(huán)輸出*組成的菱形實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Python實現(xiàn)批量將圖像png格式轉為npy格式
在進行深度學習處理時,有些的代碼處理的數(shù)據(jù)格式為npy,但是常常有的數(shù)據(jù)格式為png,因此本文就來介紹一下Python如何實現(xiàn)圖像批量png格式轉為npy格式,需要的可以參考下2023-12-12
tensorflow使用CNN分析mnist手寫體數(shù)字數(shù)據(jù)集
這篇文章主要介紹了tensorflow使用CNN分析mnist手寫體數(shù)字數(shù)據(jù)集,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-06-06
PyTorch與PyTorch?Geometric的安裝過程
這篇文章主要介紹了PyTorch與PyTorch?Geometric的安裝,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04
深度解析Django REST Framework 批量操作
這篇文章主要介紹了深度解析Django REST Framework批量操作,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-05-05
在Python中使用Protocol?Buffers的詳細介紹
本文詳細介紹了協(xié)議緩沖區(qū)(Protocol Buffers)在Python中的應用,包括其定義、序列化和解析過程,協(xié)議緩沖區(qū)是一種靈活且高效的自動化解決方案,本文包括了如何將地址簿應用程序的個人詳細信息寫入文件的示例代碼,并提供了相應的下載和安裝指導,感興趣的朋友一起看看吧2024-10-10

