1.列印此號碼:
1 2 3 4 5 5 4 3 2 1
no = 1 top = 5 direction = 1 while no>0: print(no,end= ' ') if no == top: print(no,end=' ') direction = -1 no = no + direction
1 2 3 4 5 5 4 3 2 1
2.猜數字遊戲:
import random system_no = random.randint(1,20) while True: guess = int(input("Enter the No. ")) if guess == system_no: print("Hurray! I got the number!! ") break elif guess > system_no: print("Your guess is too high! ") else: print("Your guess is too Low! ")
Enter the No. 15 Your guess is too Low! Enter the No. 17 Your guess is too Low! Enter the No. 19 Hurray! I got the number!!
3.找出數字中最大的數字
no = int(input("Enter no. ")) max_no = 0 while no>0: rem = no%10 if rem>max_no: max_no = rem no//=10 print(max_no)
Enter no. 2357 7
4.找出數字中最小的數字
no = int(input("Enter no. ")) min_no = 9 while no>0: rem = no%10 if rem<min_no: min_no="rem" no print> <pre class="brush:php;toolbar:false">Enter no. 2357 2
5.Collatz 序列
編寫一個程序,列印給定數字的 Collatz 序列,直到達到 1。
規則:
If the number is even: n=n/2 If the number is odd: n=3n+1.
def even_odd(no): while no>0: num=no%10 if num%2==0: even=num/2 else: odd=3*num+1 no=no//10 return even,odd no=int(input("Enter the number:")) even,odd=even_odd(no) print("even number:",even) print("odd number:",odd)
Enter the number:12 even number: 1.0 odd number: 4
以上是日 - 循環練習的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Linux終端中查看Python版本時遇到權限問題的解決方法當你在Linux終端中嘗試查看Python的版本時,輸入python...

本文解釋瞭如何使用美麗的湯庫來解析html。 它詳細介紹了常見方法,例如find(),find_all(),select()和get_text(),以用於數據提取,處理不同的HTML結構和錯誤以及替代方案(SEL)

本文比較了Tensorflow和Pytorch的深度學習。 它詳細介紹了所涉及的步驟:數據準備,模型構建,培訓,評估和部署。 框架之間的關鍵差異,特別是關於計算刻度的

本文指導Python開發人員構建命令行界面(CLIS)。 它使用Typer,Click和ArgParse等庫詳細介紹,強調輸入/輸出處理,並促進用戶友好的設計模式,以提高CLI可用性。

在使用Python的pandas庫時,如何在兩個結構不同的DataFrame之間進行整列複製是一個常見的問題。假設我們有兩個Dat...

本文討論了諸如Numpy,Pandas,Matplotlib,Scikit-Learn,Tensorflow,Tensorflow,Django,Blask和請求等流行的Python庫,並詳細介紹了它們在科學計算,數據分析,可視化,機器學習,網絡開發和H中的用途

文章討論了虛擬環境在Python中的作用,重點是管理項目依賴性並避免衝突。它詳細介紹了他們在改善項目管理和減少依賴問題方面的創建,激活和利益。


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

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

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

記事本++7.3.1
好用且免費的程式碼編輯器

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中