日間循環練習
這裡有一些針對數字的 while 循環問題可以練習:
基本問題
1.印數字
寫一個程序,使用 while 循環列印從 1 到 10 的數字。
def print_number(no): num=1 while num<=no: print(num, end=" ") num+=1 print_number(10)
1 2 3 4 5 6 7 8 9 10
2.N 個數字的和
寫一個程序,使用 while 迴圈計算前 NN 個自然數的和。
def sum_of_number(no): num=1 total=0 while num<=no: total=total+num num+=1 return total no=int(input("Sum of the number:")) print(sum_of_number(no))
Sum of the number:10 55
3.偶數
寫一個程序,使用 while 迴圈列印 1 到 50 之間的所有偶數。
def print_even_number(no): num=2 while num<=no: print(num, end=" ") num+=2 print_even_number(50)
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
4.奇數
寫一個程式來列印 1 到 NN 之間的所有奇數。
def print_odd_number(no): num=1 while num<=no: print(num, end=" ") num+=2 return no no=int(input("Enter the number:")) print_odd_number(no)
Enter the number:20 1 3 5 7 9 11 13 15 17 19
5.倒數
編寫一個程序,使用 while 迴圈以相反的順序列印從 20 到 1 的數字。
def print_reverse_number(no): num=20 while num>=no: print(num, end=" ") num-=1 print_reverse_number(1)
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
中級問題
1.階乘計算
寫一個程序,使用 while 迴圈計算給定數字的階乘。
def find_factorial(num): no=1 factorial=1 while no<=num: factorial=factorial*no no+=1 return factorial num=int(input("Enter the number:")) print(find_factorial(num))
Enter the number:5 120
- 數字總和 寫一個程式來計算給定數字的數字總和(例如,123 → 1 2 3=6)。
def sum_of_digits(num): sum=0 while num>0: sum=sum+num%10 num=num//10 return sum num=int(input("Enter the number:")) print(sum_of_digits(num))
Enter the number:123 6
3.數數字
編寫一個程式來計算給定數字的位數(例如,12345 → 5 位數字)。
def count_of_digits(num): count=0 while num>0: num=num//10 count+=1 return count num=int(input("Enter the number:")) print(count_of_digits(num))
Enter the number:12345 5
4.反轉數字
寫一個程式來反轉給定的數字(例如 123 → 321)。
def reverse_number(num): reverse=0 while num>0: reverse=reverse*10+num%10 num=num//10 return reverse num=int(input("Enter the number:")) print(reverse_number(num))
Enter the number:123 321
5.乘法表
寫一個程序,使用 while 迴圈列印給定數字 nn 的乘法表。
def multiply(num): no=1 while no<=15: print(no,"*",num,"=",no*num , end="\n") no+=1 num=int(input("Enter the number:")) multiply(num)
Enter the number:12 1 * 12 = 12 2 * 12 = 24 3 * 12 = 36 4 * 12 = 48 5 * 12 = 60 6 * 12 = 72 7 * 12 = 84 8 * 12 = 96 9 * 12 = 108 10 * 12 = 120 11 * 12 = 132 12 * 12 = 144 13 * 12 = 156 14 * 12 = 168 15 * 12 = 180
高階問題
1.檢查回文
寫一個程式來檢查給定的數字是否為回文(例如,121→回文,123→不是回文)。
def palindrome(num): count=0 while num>0: count=count*10+num%10 num=num//10 return count num=int(input("Enter the number:")) result=palindrome(num) if result==num: print("Palindrome") else: print("Not palindrome")
Enter the number:121 Palindrome Enter the number:123 Not palindrome
*2.找到力量*
def find_power(base,power): result=1 while power>=1: result=result*base power-=1 return result base=int(input("Enter the base number:")) power=int(input("Enter the power number:")) result=find_power(base,power) print(result)
Enter the base number:2 Enter the power number:5 32
3.阿姆斯壯數
寫一個程式來檢查給定的數字是否為阿姆斯壯數(例如,153 → 13 53 33=15313 53 33=153)。
def count_of_digits(num): count=0 while num>0: num=num//10 count+=1 return count def find_power(base,power): result=1 while power>=1: result=result*base power-=1 return result def find_armstrong(num,count): armstrong=0 while num>0: rem=num%10 result= find_power(rem,count) armstrong=armstrong+result num=num//10 return armstrong num=int(input("Enter the number:")) count=count_of_digits(num) armstrong_result=find_armstrong(num,count) if armstrong_result==num: print("Armstrong") else: print("Not armstrong")
Enter the number:123 Not armstrong Enter the number:153 Armstrong
4.偶數和奇數位置總和:
def sum_of_even_odd(num): odd=0 even=0 index=0 while index<len(num): digit=int(num[index]) if index%2==0: even=even+digit else: odd=odd+digit index+=1 return even,odd num=input("Enter the number:") even,odd=sum_of_even_odd(num) print("sum of even number:",even) print("sum of odd number:",odd)
Enter the number:12345 sum of even number: 9 sum of odd number: 6
以上是日間循環練習的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

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

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Python更易學且易用,C 則更強大但複雜。 1.Python語法簡潔,適合初學者,動態類型和自動內存管理使其易用,但可能導致運行時錯誤。 2.C 提供低級控制和高級特性,適合高性能應用,但學習門檻高,需手動管理內存和類型安全。

要在有限的時間內最大化學習Python的效率,可以使用Python的datetime、time和schedule模塊。 1.datetime模塊用於記錄和規劃學習時間。 2.time模塊幫助設置學習和休息時間。 3.schedule模塊自動化安排每週學習任務。

Python在開發效率上優於C ,但C 在執行性能上更高。 1.Python的簡潔語法和豐富庫提高開發效率。 2.C 的編譯型特性和硬件控制提升執行性能。選擇時需根據項目需求權衡開發速度與執行效率。

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

每天學習Python兩個小時是否足夠?這取決於你的目標和學習方法。 1)制定清晰的學習計劃,2)選擇合適的學習資源和方法,3)動手實踐和復習鞏固,可以在這段時間內逐步掌握Python的基本知識和高級功能。

Python和C 各有優勢,選擇應基於項目需求。 1)Python適合快速開發和數據處理,因其簡潔語法和動態類型。 2)C 適用於高性能和系統編程,因其靜態類型和手動內存管理。

Python在自動化、腳本編寫和任務管理中表現出色。 1)自動化:通過標準庫如os、shutil實現文件備份。 2)腳本編寫:使用psutil庫監控系統資源。 3)任務管理:利用schedule庫調度任務。 Python的易用性和豐富庫支持使其在這些領域中成為首選工具。

Python在Web開發中的關鍵應用包括使用Django和Flask框架、API開發、數據分析與可視化、機器學習與AI、以及性能優化。 1.Django和Flask框架:Django適合快速開發複雜應用,Flask適用於小型或高度自定義項目。 2.API開發:使用Flask或DjangoRESTFramework構建RESTfulAPI。 3.數據分析與可視化:利用Python處理數據並通過Web界面展示。 4.機器學習與AI:Python用於構建智能Web應用。 5.性能優化:通過異步編程、緩存和代碼優
