搜尋
首頁後端開發Python教學Collat​​z 序列、逗號代碼、字元圖網格

Collat​​z 序列、逗號代碼、字元圖網格

Jun 23, 2017 am 11:44 AM
程式碼字元序列逗號

1.collat​​z序列

  寫一個名為 collat​​z()的函數,它 有一個名為 number 的參數。如果參數是偶數, 那麼 collat​​z()就會印出 number // 2,並傳回該值。如果 number 是奇數,collat​​z()就打 印並回傳 3 * number + 1。 然後寫一個程序,讓使用者輸入一個整數,並不斷對這個數呼叫 collat​​z(),直 到函數回傳值1。

 1 #!/usr/bin/env python3 2 # -*- coding:utf-8 -*- 3  4 def collatz(number): 5     print(number) 6     if number ==1: 7         return number 8     elif number % 2 ==0: 9         return collatz(number//2)10     else:11         return collatz(3*number +1)12 13 A = int(input('Input a number: '))14 while True:15     if collatz(A) != 1:16         continue17     else:18         break

輸出結果:

 1 Input a number: 6 2 6 3 3 4 10 5 5 6 16 7 8 8 4 9 210 1

#2.逗號代碼

假定有下面這樣的列表: spam = [' apples', 'bananas', 'tofu', 'cats'] <br>   寫一個函數,它以一個列表值作為參數,傳回一個字串。此字串包含所 有表項,表項之間以逗號和空格分隔,並在最後一個表項之前插入and。例如,將前面的 spam 列表傳遞給函數,將返回'apples, bananas, tofu, and cats'。但你的函數應該能夠處理傳遞給它的任何列表。

<br>
#!/usr/bin/env python3<br># -*- coding:utf-8 -*-<br><br>def func(spam):<br>    spam[-1]='and'+ ' ' + spam[-1]<br>for i in range(len(spam)):<br>print(spam[i], end=',')<br><br><br>spam = ['apple', 'bananas', 'tofu', 'cats', 'dog']<br>func(spam)<br>#输出结果<br>apple,bananas,tofu,cats,and dog,<br>

3.字元圖網格

假定有一個列表的列表,內層列表的每個值都是包含一個字元的字串,像這樣:

grid =[['.', '.', '.', '.', '.', '.'], 

['.', 'O' , 'O', '.', '.', '.'], 

['O', 'O', 'O', 'O', '.', '.'],

['O', 'O', 'O', 'O', 'O', '.'], 

#['.', 'O', 'O' , 'O', 'O', 'O'],  

['O', 'O', 'O', 'O', 'O', '.'],  

#['O', 'O', 'O', 'O', '.', '.'], 

['.', 'O', 'O', '.' , '.', '.'],        

#['.', '.', '.', '.', '.', '.']]

#你可以認為grid[x][y]是一幅「圖」在x、y 座標處的字符,該圖由文字字符組成。原點(0, 0)在左上角,向右 x 座標增加,向下 y 座標增加。 複製前面的網格值,寫程式用它列印出圖像。

..OO.OO..

.OOOOOOO.

.OOOOOOO.

..OOOOO..

.. .OOO...

....O....

    grid =     [, , , , ,      [, , , , ,      [, , , , ,      [, , , , ,      [, , , , ,      [, , , , ,      [, , , , ,      [, , , , ,      [, , , , ,  #嵌套循环  n       m          (grid[m][n], end=     ()#换行

#输出结果
..OO.OO..

.OOOOOOO.

.OOOOOOO.

..OOOOO..

...OOO...

....O....

<br> 

以上是Collat​​z 序列、逗號代碼、字元圖網格的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
在Python陣列上可以執行哪些常見操作?在Python陣列上可以執行哪些常見操作?Apr 26, 2025 am 12:22 AM

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

在哪些類型的應用程序中,Numpy數組常用?在哪些類型的應用程序中,Numpy數組常用?Apr 26, 2025 am 12:13 AM

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

您什麼時候選擇在Python中的列表上使用數組?您什麼時候選擇在Python中的列表上使用數組?Apr 26, 2025 am 12:12 AM

useanArray.ArarayoveralistinpythonwhendeAlingwithHomoGeneData,performance-Caliticalcode,orinterfacingwithccode.1)同質性data:arraysSaveMemorywithTypedElements.2)績效code-performance-calitialcode-calliginal-clitical-clitical-calligation-Critical-Code:Arraysofferferbetterperbetterperperformanceformanceformancefornallancefornalumericalical.3)

所有列表操作是否由數組支持,反之亦然?為什麼或為什麼不呢?所有列表操作是否由數組支持,反之亦然?為什麼或為什麼不呢?Apr 26, 2025 am 12:05 AM

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactsperformance.2)listssdonotguaranteeconecontanttanttanttanttanttanttanttanttanttimecomplecomecomplecomecomecomecomecomecomplecomectacccesslectaccesslecrectaccesslerikearraysodo。

您如何在python列表中訪問元素?您如何在python列表中訪問元素?Apr 26, 2025 am 12:03 AM

toAccesselementsInapythonlist,useIndIndexing,負索引,切片,口頭化。 1)indexingStartSat0.2)否定indexingAccessesessessessesfomtheend.3)slicingextractsportions.4)iterationerationUsistorationUsisturessoreTionsforloopsoreNumeratorseforeporloopsorenumerate.alwaysCheckListListListListlentePtotoVoidToavoIndexIndexIndexIndexIndexIndExerror。

Python的科學計算中如何使用陣列?Python的科學計算中如何使用陣列?Apr 25, 2025 am 12:28 AM

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

您如何處理同一系統上的不同Python版本?您如何處理同一系統上的不同Python版本?Apr 25, 2025 am 12:24 AM

你可以通過使用pyenv、venv和Anaconda來管理不同的Python版本。 1)使用pyenv管理多個Python版本:安裝pyenv,設置全局和本地版本。 2)使用venv創建虛擬環境以隔離項目依賴。 3)使用Anaconda管理數據科學項目中的Python版本。 4)保留系統Python用於系統級任務。通過這些工具和策略,你可以有效地管理不同版本的Python,確保項目順利運行。

與標準Python陣列相比,使用Numpy數組的一些優點是什麼?與標準Python陣列相比,使用Numpy數組的一些優點是什麼?Apr 25, 2025 am 12:21 AM

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基於基於duetoc的iMplation,2)2)他們的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函數函數函數函數構成和穩定性構成和穩定性的操作,製造

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。