Python打印“菱形”星號代碼方法
更新時間:2018年02月05日 14:42:17 投稿:laozhang
本篇文章通過代碼實例給大家詳細分析了Python打印“菱形”星號代碼方法,對此有需要的朋友參考下吧。
本人是一名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)
運行結(jié)果:
相關(guān)文章
Python?OpenCV的基本使用及相關(guān)函數(shù)
這篇文章主要介紹了Python-OpenCV的基本使用和相關(guān)函數(shù)介紹,主要包括圖像的讀取保存圖像展示問題,結(jié)合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-05-05python實現(xiàn)在多維數(shù)組中挑選符合條件的全部元素
今天小編就為大家分享一篇python實現(xiàn)在多維數(shù)組中挑選符合條件的全部元素,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11Python報錯ModuleNotFoundError: No module named&
在嘗試導(dǎo)入TensorBoard模塊時,你可能會遇到ModuleNotFoundError: No module named 'tensorboard'的錯誤,下面我們來分析這個問題并提供解決方案,需要的朋友可以參考下2024-09-09