新聞中心
創(chuàng)新互聯(lián)python教程:

習(xí)水ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!
編寫一個(gè) Python 程序,使用 While 循環(huán)、For 循環(huán)和帶有示例的函數(shù)來(lái)查找一個(gè)數(shù)的因數(shù)。
使用 While 循環(huán)查找一個(gè)數(shù)的因數(shù)的 Python 程序
這個(gè) Python 程序允許用戶輸入任何整數(shù)值。接下來(lái),這個(gè) Python 程序使用 While 循環(huán)找到這個(gè)數(shù)字的因數(shù)。記住,可以被給定整數(shù)整除的整數(shù)(它意味著余數(shù)= 0)稱為因數(shù)。
number = int(input("Please Enter any Number: "))
value = 1
print("Factors of a Given Number {0} are:".format(number))
while (value <= number):
if(number % value == 0):
print("{0}".format(value))
value = value + 1Please Enter any Number: 4
Factors of a Given Number 4 are:
1
2
4在 Python while 循環(huán)中,有一個(gè) If 語(yǔ)句來(lái)檢查被值整除的數(shù)字是否正好等于 0。如果為真,它將打印該整數(shù)。否則,它會(huì)跳過(guò)該整數(shù)并檢查下一個(gè)整數(shù)。這里,數(shù)字= 4,數(shù)值= 1
第一次迭代 (值< =數(shù)字)–表示(1 < = 4)為真 現(xiàn)在,檢查 if 條件 if(數(shù)字%值= = 0)=>(4% 1 = = 0)–該條件為真。所以,1 打印
價(jià)值=價(jià)值+1–意味著價(jià)值變成 2
第二次迭代 值= 2,數(shù)字= 4–如果(4% 2 = = 0)–該條件為真,則表示(2 < = 4)為真 。所以,打印了 2 張
第三次迭代 值= 3,數(shù)字= 4–如果(3% 2 = = 0)–條件為 FLASE,則表示(3 < = 4)為真 。所以,跳過(guò) 3 個(gè)
第四次迭代 i = 4,Number = 4–如果(4% 4 = = 0)–條件為真,則表示(4 < = 4)為真 。4 印
接下來(lái),值變?yōu)?5–表示條件(5 <= 4)為假。所以,循環(huán)終止。給定 4 的因數(shù)= 1,2,4。
使用 For 循環(huán)尋找一個(gè)數(shù)的因數(shù)的 Python 程序
在一個(gè)數(shù)字程序的這些因素中,我們只是將 While Loop 替換為 For Loop 。
number = int(input("Please Enter any Number: "))
print("Factors of a Given Number {0} are:".format(number))
for value in range(1, number + 1):
if(number%value == 0):
print("{0}".format(value))
用函數(shù)求數(shù)的因數(shù)的 Python 程序
這個(gè) Python 因數(shù)的一個(gè)數(shù)字程序與上面的例子相同。但是在這個(gè) Python 程序中,我們通過(guò)定義一個(gè)名為 Find_factors 的新函數(shù)來(lái)分離因數(shù)邏輯。
def Find_Factors(number):
for value in range(1, number + 1):
if(number % value == 0):
print("{0}".format(value))
num = int(input("Please Enter any Number: "))
print("Factors of a Given Number {0} are:".format(num))
Find_Factors(num)Please Enter any Number: 222
Factors of a Given Number 222 are:
1
2
3
6
37
74
111
222 網(wǎng)站標(biāo)題:Python程序:尋找數(shù)字因數(shù)
網(wǎng)頁(yè)URL:http://fisionsoft.com.cn/article/cdopjic.html


咨詢
建站咨詢
