返回列表 發帖

if...elif...else

本帖最後由 鄭繼威 於 2022-7-19 10:50 編輯

多向判斷式語法
if (條件式一) :
    程式區塊一
elif (條件式二):
    程式區塊二
elif (條件式三):
    程式區塊三
.........
else:
    else的程式區塊


  1. score=int(input("請輸入你的成績: "))
  2. if score>=90 and score<=100:
  3.     print("優等!")
  4. elif score>=80 and score<90:
  5.     print("甲等!")
  6. elif score>=70 and score<80:
  7.     print("乙等!")
  8. elif score>=60 and score<70:
  9.     print("丙等!")
  10. elif score>=0 and score<60:
  11.     print("不及格!")
  12. else:
  13.     print("輸入錯誤!")
複製代碼
  1. score=int(input("請輸入你的成績: "))
  2. if score>100:
  3.     print("輸入錯誤!")
  4. elif score>=90:
  5.     print("優等!")
  6. elif score>=80:
  7.     print("甲等!")
  8. elif score>=70:
  9.     print("乙等!")
  10. elif score>=60:
  11.     print("丙等!")
  12. elif score>=0:
  13.     print("不及格!")
  14. else:
  15.     print("輸入錯誤!")
複製代碼

  1. a=int(input("請輸入成績:"))
  2. print("你的成績",a)
  3. #90~100
  4. if(a>=90 and a<=100):
  5.     print("優等!")
  6. #80~89
  7. elif(a>=80 and a<=89):
  8.     print("甲等!")
  9. #70~79
  10. elif(a>=70 and a<=79):
  11.     print("乙等!")
  12. #60~69
  13. elif(a>=60 and a<=69):
  14.     print("丙等!")
  15. #0~59
  16. elif(a>=0 and a<=59):
  17.     print("丁等!")
  18. else:
  19.     print("輸入錯誤")
複製代碼

TOP

返回列表