3: Selection

3-5: if Statement

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

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

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

a = 8 b = 1 if a < 0: a = -a b = 2*a if not(a > 10): a *= 3 b += a print(a,b) Ex().check_if_else(missing_msg="ใช้ if ตามผังงาน") for v in range(-20, 21, 5): Ex().has_equal_output(pre_code="a="+str(v), incorrect_msg = "ผิด เช่น กรณี a = " + str(v))
แบบฝึกหัด 3-5 ข้อที่ 2

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

a = 8 b = 1 if a < 0: a = -a b = 2*a if a > 10: a *= 3 b += a print(a,b) Ex().check_if_else(missing_msg="ใช้ if ตามผังงาน") for v in range(-5, 16, 5): Ex().has_equal_output(pre_code="a="+str(v), incorrect_msg = "ผิด เช่น กรณี a = " + str(v))
แบบฝึกหัด 3-5 ข้อที่ 3

มีตัวแปร t เก็บสตริงอยู่แล้ว โดยที่ t มีตัวอักษรภายใน 5 ตัว จงแสดงจำนวนตัวใน t ที่เป็นสระ (นับทั้งตัวเล็กและตัวใหญ่) เช่น Annie มีสระ 3 ตัว

t = "12345" # ให้ c เก็บจำนวนสระ เริ่มต้นมีค่า 0 # ถ้าตัวที่ index 0 เป็นสระ ก็เพิ่ม c อีก 1 # แล้วก็ตรวจสอบตัวต่าง ๆ ที่เหลือ c = 0 vowels = "aeiouAEIOU" if t[0] in vowels: c += 1 if t[1] in vowels: c += 1 if t[2] in vowels: c += 1 if t[3] in vowels: c += 1 if t[4] in vowels: c += 1 print(c) for v in ['12345', 'ouiea', 'Jerry', 'Euler', 'Junes', 'April', 'black']: Ex().has_equal_output(pre_code = "t='"+v+"'", incorrect_msg = "ผิด เช่น t = '" + v + "'") v = v.lower() Ex().has_equal_output(pre_code = "t='"+v+"'", incorrect_msg = "ผิด เช่น t = '" + v + "'")
ให้ c = 0 เป๋นตัวแปรเก็บจำนวนสระ ถ้าตัวแรกใน t เป็นสระ ก็เพิ่มค่า c, ทำแบบนี้กับตัวอื่น ๆ ใน t
แบบฝึกหัด 3-5 ข้อที่ 4

มีตัวแปร text เก็บสตริงอยู่แล้ว อยากทราบว่าใน text มีตัวอักษรที่เป็นสระอะไรบ้าง (เฉพาะตัวเล็ก) เช่น Mission ให้แสดง io, understand ให้แสดง aeu (แสดงสระที่มี ตามลำดับตัวอักษร) ถ้าไม่มีสระเลยให้แสดง No vowels

text = "uoiea" r = "" c = "a" if c in text: r += c c = "e" if c in text: r += c c = "i" if c in text: r += c c = "o" if c in text: r += c c = "u" if c in text: r += c if r == "": print("No vowels") else: print(r) for v in ['Annotation', "ABBA", "ideal idea", "Europa", "UOIEA", "XYZ"]: Ex().has_equal_output(pre_code = "text='"+v+"'", incorrect_msg = "ผิด เช่น text = '" + v + "'") v = v.lower() Ex().has_equal_output(pre_code = "text='"+v+"'", incorrect_msg = "ผิด เช่น text = '" + v + "'")
ให้ r = "" ถ้ามี a ใน text ก็เอา a ไปต่อใน r ทำแบบนี้กับสระตัวอื่น ...