Python教程(三)

條件判斷

  • if-else

說明:elif 是 else if 的縮寫,在每一個 if、elif(else if)、else 語句后面都要加上冒號

  • 示例代碼
scoreStr = input("Please input your score:");
score = int(scoreStr);
PASS_SCORE = 60;
WELL_SCORE = 80;
EXCELLENT_SCORE = 90;
MAX_SCORE_LIMIT=100;
MIN_SCORE_LIMIT=0;
if score<MIN_SCORE_LIMIT:
    print("Invalid score : less than min score limitation.")
elif score<PASS_SCORE:
    print("Sorry,you've failed to pass the exam.")
elif score<WELL_SCORE:
    print("Congratulations,you've passed the exam.")
elif score<EXCELLENT_SCORE:
    print("Pretty good,your score is favorable.")
elif score<MAX_SCORE_LIMIT:
    print("You've got a excellent score.")
elif score==MAX_SCORE_LIMIT:
    print("You've got the full mark.")
else:
    print("Invalid score : greater than max score limitation.")

循環(huán)

  • for
  • 示例代碼
numberSeq=[1,3,5,7,8]
sum = 0
for x in numberSeq:
    sum+=x
print("sum=%d" % sum)
  • while
  • 示例代碼
n = 100
sum = 0
while n>0 :
    sum+=n
    n-=1
print("sum=%d" % sum)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗。 張土汪:刷leetcod...
    土汪閱讀 12,768評論 0 33
  • Python 是一種相當(dāng)高級的語言,通過 Python 解釋器把符合語法的程序代碼轉(zhuǎn)換成 CPU 能夠執(zhí)行的機(jī)器碼...
    Python程序媛閱讀 1,941評論 0 3
  • http://python.jobbole.com/85231/ 關(guān)于專業(yè)技能寫完項目接著寫寫一名3年工作經(jīng)驗的J...
    燕京博士閱讀 7,616評論 1 118
  • 感恩天地滋養(yǎng),宇宙永恒;感恩大自然無私的愛;感恩祖先傳承,歷代宗親護(hù)佑;感恩國泰民安,繁榮昌盛;感恩父母生養(yǎng)大恩,...
    天門金珠瑜伽閱讀 174評論 1 1
  • 鄭鹿成閱讀 1,451評論 0 0