新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Python程序:計(jì)算10個(gè)數(shù)的和并跳過(guò)負(fù)數(shù)
創(chuàng)新互聯(lián)python教程:

成都創(chuàng)新互聯(lián)公司主要從事網(wǎng)站設(shè)計(jì)制作、網(wǎng)站制作、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)屏山,十載網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來(lái)電咨詢建站服務(wù):13518219792
寫(xiě)一個(gè) Python 程序,求 10 個(gè)數(shù)的和,跳過(guò)負(fù)數(shù)。在這個(gè) Python 示例中,for 循環(huán)范圍允許輸入 10 個(gè)數(shù)字,if 條件檢查是否有負(fù)數(shù)。如果為真,continue 語(yǔ)句將跳過(guò)該數(shù)字,不將其添加到 posSum 變量中。
posSum = 0
print("Please Enter 10 Numbersto Find Positive Sum\n")
for i in range(1, 11):
num = int(input("Number %d = " %i))
if num < 0:
continue
posSum = posSum + num
print("The Sum of 10 Numbers by Skipping Negative Numbers = ", posSum)
這個(gè) Python 程序找到 10 個(gè)正數(shù)的和,并使用 while 循環(huán)跳過(guò)負(fù)數(shù)。
posSum = 0
print("Please Enter 10 Numbersto Find Positive Sum\n")
i = 1
while(i <= 10):
num = int(input("Number %d = " %i))
if num < 0:
i = i + 1
continue
posSum = posSum + num
i = i + 1
print("The Sum of 10 Numbers by Skipping Negative Numbers = ", posSum)
Please Enter 10 Numbersto Find Positive Sum
Number 1 = 10
Number 2 = -12
Number 3 = 15
Number 4 = -24
Number 5 = 100
Number 6 = -90
Number 7 = 120
Number 8 = -34
Number 9 = 80
Number 10 = 70
The Sum of 10 Numbers by Skipping Negative Numbers = 395 本文標(biāo)題:Python程序:計(jì)算10個(gè)數(shù)的和并跳過(guò)負(fù)數(shù)
文章鏈接:http://m.fisionsoft.com.cn/article/cdipici.html


咨詢
建站咨詢
