返回列表 發帖

if...elif...else

本帖最後由 鄭繼威 於 2022-8-19 11:05 編輯

多向判斷式語法
if (條件式一) :
    程式區塊一
elif (條件式二):
    程式區塊二
elif (條件式三):
    程式區塊三
.........
else:
    else的程式區塊
[補充]
if...  只能有一個(放在第一個)
elif...  可以很多個(放在中間)
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("輸入錯誤!")
複製代碼

本帖最後由 彭郁程 於 2022-8-19 10:34 編輯

1.
  1. score=int(input("請輸入你的成績"))
  2. print("你的成績為",score,sep="")
  3. if (score<60) :
  4.   print("不及格")
  5. elif(score>=60 and score<=100):
  6.   print("及格!")
  7. else:
  8.     print("無效!最高分數為100!")
  9.    
複製代碼
2.
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri Aug 19 10:28:38 2022

  4. @author: student
  5. """

  6. score=int(input("請輸入你的成績: "))
  7. if score>=90 and score<=100:
  8.     print("優等!")
  9. elif score>=80 and score<90:
  10.     print("甲等!")
  11. elif score>=70 and score<80:
  12.     print("乙等!")
  13. elif score>=60 and score<70:
  14.     print("丙等!")
  15. elif score>=0 and score<60:
  16.     print("不及格!")
  17. else:
  18.     print("輸入錯誤!")
複製代碼
3.
  1. a=int(input("請輸入國語成績 "))
  2. b=int(input("請輸入英文成績: "))
  3. c=int(input("請輸入數學成績: "))
  4. sum=a+b+c
  5. avgr=sum/3
  6. print("總分%d平均:%.2f"%(sum,avgr))


  7. if avgr>=90 and avgr<=100:
  8.     print("優等!")
  9. elif avgr>=80 and avgr<90:
  10.     print("甲等!")
  11. elif avgr>=70 and avgr<80:
  12.     print("乙等!")
  13. elif avgr>=60 and avgr<70:
  14.     print("丙等!")
  15. elif avgr>=0 and avgr<60:
  16.     print("不及格!")
  17. else:
  18.     print("輸入錯誤!")
複製代碼

TOP

  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("輸入錯誤!")
複製代碼

TOP

  1. a=int(input("請輸入你的成績:"))

  2. #100超棒
  3. if a==100:
  4.     print("超棒的")
  5. #90~99
  6. elif a>=90 and a<=99:
  7.     print("優等")
  8. #80~89
  9. elif a>=80 and a<=89:
  10.     print("甲等")
  11. #70~79
  12. elif a>=70 and a<=79:
  13.     print("乙等")
  14. #60~69
  15. elif a>=60 and a<=69:
  16.     print("丙等")
  17. #60以下0以上
  18. elif a>=0 and a<=59:
  19.     print("不及格")
  20. else:
  21.     print("輸入錯誤")
複製代碼

TOP

  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("輸入錯誤!")
複製代碼

TOP

本帖最後由 姜宏叡 於 2022-8-19 11:13 編輯
  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("輸入錯誤")
複製代碼

TOP

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

TOP

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

TOP

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

TOP

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

TOP

返回列表