Python打印“菱形”星號(hào)代碼方法
更新時(shí)間:2018年02月05日 14:42:17 投稿:laozhang
本篇文章通過代碼實(shí)例給大家詳細(xì)分析了Python打印“菱形”星號(hào)代碼方法,對(duì)此有需要的朋友參考下吧。
本人是一名python初學(xué)者,剛剛看到一道有趣的python問題,“用python如何在編譯器中打印出菱形圖案?”
因此決定嘗試一下,代碼不多,僅供參考。
代碼
def printStar(intNum): s = "*" spaceLength = intNum blockCount = int(intNum/2+1) for i in range(spaceLength): result = s.rjust(blockCount) if i >= int(spaceLength/2): print(result) s = s[2:] blockCount -= 1 else: print(result) s = s+(2*"*") blockCount += 1 def oddOReven(intNum): if intNum%2 == 0: print("please input a odd num data") else: printStar(intNum) if __name__ == '__main__': while True: try: intNum = eval(input("please input a odd num data\n")) oddOReven(intNum) except BaseException as e: print("Please input as 1/2/3... Errorcode:%s" % e)
運(yùn)行結(jié)果:
相關(guān)文章
Python?OpenCV的基本使用及相關(guān)函數(shù)
這篇文章主要介紹了Python-OpenCV的基本使用和相關(guān)函數(shù)介紹,主要包括圖像的讀取保存圖像展示問題,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05python實(shí)現(xiàn)在多維數(shù)組中挑選符合條件的全部元素
今天小編就為大家分享一篇python實(shí)現(xiàn)在多維數(shù)組中挑選符合條件的全部元素,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11Python報(bào)錯(cuò)ModuleNotFoundError: No module named&
在嘗試導(dǎo)入TensorBoard模塊時(shí),你可能會(huì)遇到ModuleNotFoundError: No module named 'tensorboard'的錯(cuò)誤,下面我們來分析這個(gè)問題并提供解決方案,需要的朋友可以參考下2024-09-09基于Python實(shí)現(xiàn)貪吃蛇小游戲(附源碼)
本次我們將編寫一個(gè)貪吃蛇的游戲。通過鍵盤上、下、左、右控制小蛇上、下、左、右移動(dòng),吃到食物后長度加1;蛇頭碰到自身或窗口邊緣,游戲失敗,需要的可以參考一下2022-11-11詳解K-means算法在Python中的實(shí)現(xiàn)
這篇文章主要介紹了詳解K-means算法在Python中的實(shí)現(xiàn),具有一定借鑒價(jià)值,需要的朋友可以了解下。2017-12-12