新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python程序:打印自然數(shù)
創(chuàng)新互聯(lián)python教程:

沈北新ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!
用例子寫一個 Python 程序,用 While 循環(huán)和 For 循環(huán)打印自然數(shù)。
使用 For 循環(huán)打印自然數(shù)的 Python 程序
這個用于自然數(shù)的 Python 程序允許用戶輸入任何整數(shù)值。接下來,該程序使用 For 循環(huán)打印從 1 到用戶指定值的自然數(shù)。
# Python Program to Print Natural Numbers from 1 to N
number = int(input("Please Enter any Number: "))
print("The List of Natural Numbers from 1 to {0} are".format(number))
for i in range(1, number + 1):
print (i, end = ' ')Python 自然數(shù)輸出
Please Enter any Number: 10
The List of Natural Numbers from 1 to 10 are
1 2 3 4 5 6 7 8 9 10 使用 While 循環(huán)的 Python 自然數(shù)程序
在這個顯示自然數(shù)的 Python 程序中,我們只是將 For Loop 替換為 While Loop
# Python Program to Print Natural Numbers from 1 to N
number = int(input("Please Enter any Number: "))
i = 1
print("The List of Natural Numbers from 1 to {0} are".format(number))
while ( i <= number):
print (i, end = ' ')
i = i + 1Python 自然數(shù)使用 while 循環(huán)輸出
Please Enter any Number: 25
The List of Natural Numbers from 1 to 25 are
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 顯示一定范圍內(nèi)自然數(shù)的 Python 程序
這個用于自然數(shù)的 Python 程序與第一個示例相同。但是這次,我們允許用戶輸入最小值和最大值。這意味著這個程序打印從最小到最大的自然數(shù)。
# Python Program to Print Natural Numbers within a range
minimum = int(input("Please Enter the Minimum integer Value : "))
maximum = int(input("Please Enter the Maximum integer Value : "))
print("The List of Natural Numbers from {0} to {1} are".format(minimum, maximum))
for i in range(minimum, maximum + 1):
print (i, end = ' ') 文章名稱:Python程序:打印自然數(shù)
本文網(wǎng)址:http://www.dlmjj.cn/article/dpjchid.html


咨詢
建站咨詢
