搜尋

日間循環練習

Dec 07, 2024 am 09:30 AM

Day - Looping exercises

這裡有一些針對數字的 while 循環問題可以練習:

基本問題

1.印數字
寫一個程序,使用 while 循環列印從 1 到 10 的數字。

def print_number(no):
    num=1
    while num





<pre class="brush:php;toolbar:false">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





<pre class="brush:php;toolbar:false">Sum of the number:10
55

3.偶數
寫一個程序,使用 while 迴圈列印 1 到 50 之間的所有偶數。

def print_even_number(no):
    num=2
    while num





<pre class="brush:php;toolbar:false">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





<pre class="brush:php;toolbar:false">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





<pre class="brush:php;toolbar:false">Enter the number:5
120
  1. 數字總和 寫一個程式來計算給定數字的數字總和(例如,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





<pre class="brush:php;toolbar:false">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 digit="int(num[index])" if index even="even+digit" else: odd="odd+digit" return num='input("Enter' the number: print of>





<pre class="brush:php;toolbar:false">Enter the number:12345
sum of even number: 9
sum of odd number: 6

以上是日間循環練習的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
您如何切成python陣列?您如何切成python陣列?May 01, 2025 am 12:18 AM

Python列表切片的基本語法是list[start:stop:step]。 1.start是包含的第一個元素索引,2.stop是排除的第一個元素索引,3.step決定元素之間的步長。切片不僅用於提取數據,還可以修改和反轉列表。

在什麼情況下,列表的表現比數組表現更好?在什麼情況下,列表的表現比數組表現更好?May 01, 2025 am 12:06 AM

ListSoutPerformarRaysin:1)DynamicsizicsizingandFrequentInsertions/刪除,2)儲存的二聚體和3)MemoryFeliceFiceForceforseforsparsedata,butmayhaveslightperformancecostsinclentoperations。

如何將Python數組轉換為Python列表?如何將Python數組轉換為Python列表?May 01, 2025 am 12:05 AM

toConvertapythonarraytoalist,usEthelist()constructororageneratorexpression.1)intimpthearraymoduleandcreateanArray.2)USELIST(ARR)或[XFORXINARR] to ConconverTittoalist,請考慮performorefformanceandmemoryfformanceandmemoryfformienceforlargedAtasetset。

當Python中存在列表時,使用數組的目的是什麼?當Python中存在列表時,使用數組的目的是什麼?May 01, 2025 am 12:04 AM

choosearraysoverlistsinpythonforbetterperformanceandmemoryfliceSpecificScenarios.1)largenumericaldatasets:arraysreducememoryusage.2)績效 - 臨界雜貨:arraysoffersoffersOffersOffersOffersPoostSfoostSforsssfortasssfortaskslikeappensearch orearch.3)testessenforcety:arraysenforce:arraysenforc

說明如何通過列表和數組的元素迭代。說明如何通過列表和數組的元素迭代。May 01, 2025 am 12:01 AM

在Python中,可以使用for循環、enumerate和列表推導式遍歷列表;在Java中,可以使用傳統for循環和增強for循環遍歷數組。 1.Python列表遍歷方法包括:for循環、enumerate和列表推導式。 2.Java數組遍歷方法包括:傳統for循環和增強for循環。

什麼是Python Switch語句?什麼是Python Switch語句?Apr 30, 2025 pm 02:08 PM

本文討論了版本3.10中介紹的Python的新“匹配”語句,該語句與其他語言相同。它增強了代碼的可讀性,並為傳統的if-elif-el提供了性能優勢

Python中有什麼例外組?Python中有什麼例外組?Apr 30, 2025 pm 02:07 PM

Python 3.11中的異常組允許同時處理多個異常,從而改善了並發方案和復雜操作中的錯誤管理。

Python中的功能註釋是什麼?Python中的功能註釋是什麼?Apr 30, 2025 pm 02:06 PM

Python中的功能註釋將元數據添加到函數中,以進行類型檢查,文檔和IDE支持。它們增強了代碼的可讀性,維護,並且在API開發,數據科學和圖書館創建中至關重要。

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

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

熱工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

EditPlus 中文破解版

EditPlus 中文破解版

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

DVWA

DVWA

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

MantisBT

MantisBT

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

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。