3: Selection

3-6: if-elif-elif-else

** ถ้าใช้งานบนมือถือหรือ tablet แนะนำให้ใช้ Chrome หรือ Safari เท่านั้น **

แบบฝึกหัด 3-6 ข้อที่ 1

เขียนโปรแกรมตามผังงานข้าง ๆ นี้

m = 'x' if m == 'a': t = "ant" elif m == 'b': t = "bat" elif m == 'c': t = "cat" elif m == 'd': t = "dog" else: t = "???" print(t) Ex().check_if_else(missing_msg="ใช้ if-elif-else ตามผังงาน") for v in "abcdef": Ex().has_equal_output(pre_code="m='"+v+"'", incorrect_msg = "ผิด เช่น กรณี m = '" + v + "'")
แบบฝึกหัด 3-6 ข้อที่ 2

มีตัวแปร bmi เก็บจำนวนจริง จงเขียนคำสั่งให้แสดงข้อความตามค่าของ bmi ดังแสดงในตารางข้าง ๆ นี้ (ในที่นี้ bmi เก็บค่า body mass index ไม่เป็นจำนวนลบแน่ ๆ)

bmi = 20 if bmi <= 15: msg = "Very severely underweight" elif bmi <= 16: msg = "Severely underweight" elif bmi <= 18.5: msg = "Underweight" elif bmi <= 25: msg = "Normal" elif bmi <= 30: msg = "Overweight" else: msg = "Obese" print(msg) for v in [10,15,15.5,16,17,18.5,20,25,27,30,34]: Ex().has_equal_output(pre_code="bmi="+str(v), incorrect_msg = "ผิด เช่น กรณี bmi = " + str(v))