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

創(chuàng)新互聯(lián)專注于東莞企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站設(shè)計,成都商城網(wǎng)站開發(fā)。東莞網(wǎng)站建設(shè)公司,為東莞等地區(qū)提供建站服務(wù)。全流程定制網(wǎng)站,專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
寫一個 Python 程序,使用 While 循環(huán)和 For 循環(huán)打印從 1 到 N 的奇數(shù),并舉例說明。
使用 For 循環(huán)打印從 1 到 N 的奇數(shù)的 Python 程序
這個 Python 程序允許用戶輸入最大限制值。接下來,Python 將打印奇數(shù)從 1 到用戶輸入的最大限制值。
在這個例子中,Python For Loop 確保奇數(shù)在 1 和最大極限值之間。
提示:建議大家參考 Python 奇數(shù)或偶數(shù)程序文章,了解 Python 奇數(shù)背后的邏輯。
# Python Program to Print Odd Numbers from 1 to N
maximum = int(input(" Please Enter any Maximum Value : "))
for number in range(1, maximum + 1):
if(number % 2 != 0):
print("{0}".format(number))Python 奇數(shù)用于循環(huán)和 if 語句輸出
Please Enter any Maximum Value : 10
1
3
5
7
9打印從 1 到 N 的奇數(shù)的 Python 程序
這個 Python 程序?qū)τ趶?1 到 N 的奇數(shù)的編碼同上。但是,我們將更改為循環(huán)以消除 If 塊。
如果你仔細(xì)觀察,我們從 1 開始范圍,我們使用的計數(shù)器值是 2。這意味著,對于第一次迭代次數(shù)= 1,第二次迭代次數(shù)= 3(不是 2)等等。
# Python Program to Print Odd Numbers from 1 to N
maximum = int(input(" Please Enter any Maximum Value : "))
for number in range(1, maximum + 1, 2):
print("{0}".format(number))Python 奇數(shù)用于循環(huán)輸出
Please Enter any Maximum Value : 12
1
3
5
7
9
11使用 While 循環(huán)打印奇數(shù)的 Python 程序
在這個 python 奇數(shù)程序中,我們只是將 For 循環(huán)替換為 While 循環(huán)。
# Python Program to Print Odd Numbers from 1 to N
maximum = int(input(" Please Enter the Maximum Value : "))
number = 1
while number <= maximum:
if(number % 2 != 0):
print("{0}".format(number))
number = number + 1 Please Enter the Maximum Value : 15
1
3
5
7
9
11
13
15使用 For 循環(huán)顯示從 1 到 100 的奇數(shù)的 Python 程序
這個 python 顯示奇數(shù)的程序允許用戶輸入最小值和最大值。接下來,Python 顯示最小值和最大值之間的奇數(shù)。
# Python Program to Print Odd Numbers from Minimum to Maximum
minimum = int(input(" Please Enter the Minimum Value : "))
maximum = int(input(" Please Enter the Maximum Value : "))
for number in range(minimum, maximum+1):
if(number % 2 != 0):
print("{0}".format(number)) 文章題目:Python 程序:打印從 1 到 N 的奇數(shù)
鏈接分享:http://www.dlmjj.cn/article/dhpsohi.html


咨詢
建站咨詢
