True and False and True False and False









































































- Slides: 73







ทบทวนความจำ True and False and True False and False True or False or True False or False not True False not False True 7



คำสงเงอนไข (if statement( ������ http: //www. ryt 9. com/s/prg/774090 10


ตวอยางคำสงเงอนไข (if statement(price = 40 if height <= 140: print('Hello Children!') price = 0 print('price =', price) height<=140 true print('Hello children') false price = 0 print('price =', price) 12

คำสงเงอนไข (if statement( • ������� condition false �� if condition: true statement 1 statement 2 : : statement n 13


การควบคมการไหลของโปรแกรม • โปรแกรมทใชคำสง height <= 140 if True e ls Fa ����� height = 160 120 price = 40 if height <= 140: print('Hello kids!') price = 0 print('price =', price) 15


ขอควรระวง • แนวคดเกยวกบบลอคเปนเรองสำคญ มากในภาษาไพธอน • ดงนนในการเขยนโปรแกรมควรตองระวงใน การยอหนา Ø แตเมอทำไปบอย กจะคนเคยไดไมยาก Fdskfjsdlkfslkdjfdsff fdskfsdflksdlkfdsf: fddslfldskf fdsfkdsfdsfd fdkfddfdfd fdkfdlf ๆ Good fdslkdskslkdjsld Fdskfjsdlkfslkdjfdsff fdskfsdflksdlkfdsf: fddslfldskf fdsfkdsfdsfd fdkfddfdfd fdkfdlf Bad fdslkdskslkdjsld 17







มมนกคด ตอนท 1 (ฟงกชน ( def num_candies(want, limit): if want < limit: return limit return want limit = 20 x = int(input("How many? ")) print("You should buy", num_candies(x, limit), "candies. ") 24

if – else statements ������ http: //splinedoctors. com/2009/02/hurry-up-andchoose/ 25


if - else statements • ���� if�� condition: • ��������statement T 1 statement T 2 (condition) ������: statement Tn ������ else: (statement) statement F 1 ������� T statement F 2 • �������� : (condition) statement Fn ������ 27

if – else statements • ������� �� if (condition) : true condition false statement T 1 statement T 2 : statement T 1 statement F 1 statement Tn statement T 2 statement F 2 else : statement F 1 : : statement F 2 statement Tn statement Fn : statement Fn 28


การควบคมการไหลของโปรแกรม n % 2 == 0 True print('It is an even number') e ls Fa print('It is an odd number') 30









ตวอยาง : ผลการสอบ (nestedif( �������� if score > 8: print("Good") score > 8 else: score <= 8 and score > 4 if score > 4: print("Passed") score <= 8 and score <= 4 else: print("Failed") score <= 4 39

ตวอยาง : ผลการสอบ (คำสง elif( if score > 8: print("Good") else: if score > 4: print("Passed") else: print("Failed") if score > 8: print("Good") elif score > 4: print("Passed") else: print("Failed") • โปรแกรมสามารถทำใหอานงายขนได ถาใชคำสง elif 40

if – else statements ������ http: //www. flickr. com/photos/29104098@N 00/285609610/ 41

if – else statements • if – else ใชในกรณทตอง การแบงเงอนไขใน การเลอกทำคำส งมากกวาหนงเง อนไข • ���� if�� condition 1: statement 1 elif condition 2: statement 2 elif condition 3: statement 3 : : : else: statement n 42

if – else true condition 1 false statements statement 1 true condition 2. . statement 2 • ������ if���� (condition 1) : statement 1 elif (condition 2) : statement 2 else : statement 3 false . statement 3 43



คำสงวนซำ ������ http: //gamedesignconcepts. wordpress. com/2009/07/02/level-2 -game-designiteration-and-rapid-prototyping/ 46




? Fa ls e โปรแกรมทำงานไดอยางไร count <= 8 True print('I like Bossanova') count = count + 1 50


คำสง while condition • ������ true ���� while condition: statement 1 statement 2 : statement z n false statement 2 : statement n 52







มมนกคด 6: โปรแกรม n = int(input()) total = 0 i = 1 while i <= n: total += i i += 1 print(total) 59

มมนกคด 6: ฟงกชน def sum_to(n): total = 0 i = 1 while i <= n: total += i i += 1 return total 60



มมนกคด 7: คำใบอก pwd = input("Enter password: ") while ___________: print("Sorry. ") ____________________ print("Correct. ") 63

มมนกคด 7: เฉลย pwd = input("Enter password: ") pwd != 'happy 204111' while ___________: print("Sorry. ") ________ pwd = input("Enter password: ") ________________ print("Correct. ") 64








มมนกคด 9 total = 0 n = 0 while n >= 0: n = input('Input n : ') if n >= 0: total = total + n print('total =", total) �������� ? ��������� n<0 72
