3: Selection

3-4: Membership Operator (in)

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

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

มีตัวแปร text เก็บสตริงอยู่แล้ว ถ้าใน text มี Python หรือ python ปรากฏอยู่ให้แสดง Yes ไม่เช่นนั้นแสดง No

text= "Python Programming" if "Python" in text or "python" in text: print("Yes") else: print("No") t = ["OkPythonOk", "Python is ok", "Ok Python", "JythonJa", "pyton"] for v in t: 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 + "'")
แบบฝึกหัด 3-4 ข้อที่ 2

มีตัวแปร month เก็บสตริงอยู่แล้ว ถ้า month เก็บชื่อเดือน ให้แสดง Yes ไม่เช่นนั้นก็แสดง No ชื่อเดือนมีดังนี้ January, February, March, April, May, June, July, August, September, October, November, December

month = "October" t = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] if month in t: print("Yes") else: print("No") t = ["Meenakom", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ] for v in t: Ex().has_equal_output(pre_code = "month='"+v+"'", incorrect_msg = "ผิด เช่น month = '" + v + "'") v = v.lower() Ex().has_equal_output(pre_code = "month='"+v+"'", incorrect_msg = "ผิด เช่น month = '" + v + "'")
สร้างลิสต์ที่เก็บชื่อเดือน แล้วใช้ in ในการทดสอบเป็นชื่อเดือนที่ถูกต้องหรือไม่