Monday, February 1, 2010

Python Programming

Condition Statement and Function
ประพัฒน์ สุริยผล

จากโจทย์ครั้งที่แล้ว ที่ให้เขียนโปรแกรมโดยให้แปลงส่วนที่คำนวณพื้นที่ทั้งหมด ให้แยกออกมาเป็นฟังก์ชันเพื่อให้โปรแกรมอ่านง่ายและแก้ไขได้ง่ายขึ้น เมื่อเสร็จสิ้นในส่วนนี้ โปรแกรมควรจะสามารถคำนวณพื้นที่สี่เหลี่ยม สามเหลี่ยม วงกลมและสี่เหลี่ยมคางหมูได้

มีคนส่งโปรแกรมมาให้ดูหลากหลายทีเดียว ในครั้งนี้ขอยกตัวอย่างมาให้ 6 แบบ เพื่อให้ดูว่า โปรแกรมโจทย์เดียวกัน สามารถเขียนออกมาได้หลายแบบพอสมควร บางโปรแกรมก็ทำงานไม่ถูกต้อง อาจจะลอง cut & paste ไปทดสอบรันดูได้ครับจากหัวข้อที่แล้ว ลองดูสิว่า โปรแกรมข้างล่างนี้ โปรแกรมไหนทำงานได้ถูกต้องตามที่ต้องการ โปรแกรมไหนมีข้อผิดพลาด ควรแก้ไขอย่างไร

แบบที่ 1

def calculateRectangleArea():
____print "Program to calculate Area of Rectangle"
____base = raw_input("Please enter base -> ")
____height = raw_input("Please enter height -> ")
____base_int = int(base)
____height_int = int(height)
____print "Rectangle area of base", base, "and height", height, "is", base_int*height_int

def calculateTriangleArea():
____print "Program to calculate Area of Triangle"
____base = raw_input("Please enter base -> ")
____height = raw_input("Please enter height -> ")
____base_int = int(base)
____height_int = int(height)
____print "Triangle area of base", base, "and height", height, "is", 0.5*base_int*height_int

def calculateCircleArea():
____print "Program to calculate Area of Circle"
____radius = raw_input("Please enter radius -> ")
____radius_int = int(radius)
____print "Circle area of radius", radius, "is", 3.14159*(radius_int**2)

def calculateTrapezoidsArea():
____print "Program to calculate Area of Trapezoids"
____parallel_base_top = raw_input("Please enter parallel_base_top -> ")
____parallel_base_bottom = raw_input("Please enter parallel_base_bottom -> ")
____height = raw_input("Please enter height -> ")
____parallel_base_top_int = int(parallel_base_top)
____parallel_base_bottom_int = int(parallel_base_bottom)
____height_int = int(height)
____print "Trapezoids area of parallel base top and bottom", parallel_base_top,\
__________"and", parallel_base_bottom, "and height", height, "is",\
__________1.0/2.0*(parallel_base_top_int +parallel_base_bottom_int)*height_int

choice = raw_input("Which type of area you would like to calculate (R/T)? -> ")
if choice == "R" or choice == "r":
____calculateRectangleArea()
if choice == "T" or choice == "t":
____calculateTriangleArea()
if choice == "C" or choice == "c":
____calculateCircleArea()
if choice == "Z" or choice == "z":
____calculateTrapezoidsArea()
if choice != "R" and choice != "r" and choice != "T" and choice != "t" and \
____choice != "C" and choice != "c" and choice != "Z" and choice != "z":

แบบที่ 2

def calculateRectangleArea():
____print "Program to calculate Area of Rectangle"
____base = raw_input("Please enter base -> ")
____height = raw_input("Please enter height -> ")
____base_int = int(base)
____height_int = int(height)
____print "Rectangle area of base", base, "and height", height, "is",\
__________base_int*height_int

def calculateTriangleArea():
____print "Program to calculate Area of Triangle\nTriangle area = 0.5*base*height"
____base = raw_input("Please enter base -> ")
____height = raw_input("Please enter height -> ")
____base_int = int(base)
____height_int = int(height)
____print "Triangle area of base", base, "and height", height, "is",\
__________0.5*base_int*height_int

def calculateCircleArea():
____print "Program to calculate Area of Circle\nCircle area = 3.14159 * radius^2"
____radius = raw_input("Please enter radius -> ")
____radius_int = int(radius)
____print "Circle area with radius", radius, "is", 3.14159 *radius_int**2

def calculateTrapezoidsArea():
____print "Program to calculate Area of Trapezoids" # = 0.5 *\
__________(parallelbase1+parallelbase2)* height
____parallelbase1 = raw_input("Please enter parallel base1 -> ")
____parallelbase2 = raw_input("Please enter parallel base2 -> ")
____height = raw_input("Please enter height -> ")
____parallelbase1_int = int(parallelbase1)
____parallelbase2_int = int(parallelbase2)
____height_int = int(height)
____print "Trapozoids area of parallel base1", parallelbase1, "parallel base2",\
__________parallelbase2, "and height", height, "is", 0.5*(parallelbase1_int +\
__________parallelbase2_int)*height_int

choice = raw_input("Which type of area you would like to calculate (R/T/C/Z)? -> ")
if choice == "R" or choice == "r":
____calculateRectangleArea()
if choice == "T" or choice == "t":
____calculateTriangleArea()
if choice == "C" or choice == "c":
____calculateCircleArea()
if choice == "Z" or choice == "z":
____calculateTrapezoidsArea()
if choice != "R" and choice != "r" and choice != "T" and choice != "t" and\
____choice != "C" and choice != "c" and choice != "z":
____print "Please type R,T,C or Z to choose the type of area to calculate"

แบบที่ 3

def calculateRectangleArea():
____print "Program to calculate Area of Rectangle"
____base = raw_input("Please enter base -> ")
____height = raw_input("Please enter height -> ")
____base_int = int(base)
____height_int = int(height)
____print "Rectangle area of base", base, "and height", height, "is",\
__________base_int*height_int

def calculateTriangleArea():
____print "Program to calculate Area of Triangle\nTriangle area = 0.5*base*height"
____base = raw_input("Please enter base -> ")
____height = raw_input("Please enter height -> ")
____base_int = int(base)
____height_int = int(height)
____print "Triangle area of base", base, "and height", height, "is",\
__________0.5*base_int*height_int

def calculateCircleArea():
____print "Program to calculate Area of Circle\nCircle area = 3.14159 * radius^2"
____radius = raw_input("Please enter radius -> ")
____radius_int = int(radius)
____print "Circle area with radius", radius, "is", 3.14159 *radius_int**2

def calculateTrapezoidsArea():
____print "Program to calculate Area of Trapezoids" # = 0.5 *\
__________(parallelbase1+parallelbase2)* height
____parallelbase1 = raw_input("Please enter parallel base1 -> ")
____parallelbase2 = raw_input("Please enter parallel base2 -> ")
____height = raw_input("Please enter height -> ")
____parallelbase1_int = int(parallelbase1)
____parallelbase2_int = int(parallelbase2)
____height_int = int(height)
____print "Trapozoids area of parallel base1", parallelbase1, "parallel base2",\
__________parallelbase2, "and height", height, "is", 0.5*(parallelbase1_int +\
__________parallelbase2_int)*height_int

choice = raw_input("Which type of area you would like to calculate (R/T/C/Z)? -> ")
if choice == "R" or choice == "r":
____calculateRectangleArea()
if choice == "T" or choice == "t":
____calculateTriangleArea()
if choice == "C" or choice == "c":
____calculateCircleArea()
if choice == "Z" or choice == "z":
____calculateTrapezoidsArea()
if choice != "R" and choice != "r" and choice != "T" and choice != "t" and\
____choice != "C" and choice != "c" and choice != "z":
____print "Please type R,T,C or Z to choose the type of area to calculate"

แบบที่ 4

def calculateRectangleArea():
____print "Program to calculate Area of Rectangle"
____base = raw_input("Please enter base -> ")
____height = raw_input("Please enter height -> ")
____base_int = int(base)
____height_int = int(height)
____print "Rectangle area of base=", base, "and height=", height, "is",\
__________base_int*height_int

def calculateTriangleArea():
____print "Program to calculate Area of Triangle"
____base = raw_input("Please enter base -> ")
____height = raw_input("Please enter height -> ")
____base_int = int(base)
____height_int = int(height)
____print "Triangle area of base=", base, "and height=", height, "is",\
__________0.5*base_int*height_int

def calculateCircleArea():
____print "Program to calculate Area of Circle"
____radius = raw_input("Please enter radius -> ")
____radius_int = int(radius)
____print "Circle area of radius=", radius, "is", 3.14159*(radius_int*radius_int)

def calculateTrapezoidsArea():
____print "Program to calculate Area of Trapezoids"
____parallel_base1 = raw_input("Please enter parallel base 1-> ")
____parallel_base2 = raw_input("Please enter parallel base 2-> ")
____height = raw_input("Please enter height -> ")
____parallel_base_int1 = int(parallel_base1)
____parallel_base_int2 = int(parallel_base2)
____height_int = int(height)
____print "Trapezoids area of parallel base1=", parallel_base_int1,\
__________"parallel base2=", parallel_base_int2, "and height=", height,"is",\
__________0.5*(parallel_base_int1+parallel_base_int2)*height_int

choice = raw_input("Which type of area you would like to calculate (R/T/C/Z)? -> ")
if choice == "R" or choice == "r":
____calculateRectangleArea()
if choice == "T" or choice == "t":
____calculateTriangleArea()
if choice == "C" or choice == "c":
____calculateTriangleArea()
if choice == "Z" or choice == "z":
____calculateTrapezoidsArea()
else:
____print "Please type R,T,C or Z to choose the type of area to calculate"

แบบที่ 5

def calculateRectangleArea():
____print "Program to calculate Area of Rectangle"
____base = raw_input("Please enter base -> ")
____height = raw_input("Please enter height -> ")
____base_int = int(base)
____height_int = int(height)
____print "Rectangle area of base", base, "and height", height, "is",\
__________base_int*height_int

def calculateTriangleArea():
____print "Program to calculate Area of Triangle"
____base = raw_input("Please enter base -> ")
____height = raw_input("Please enter height -> ")
____base_int = int(base)
____height_int = int(height)
____print "Triangle area of base", base, "and height", height, "is",\
__________0.5*base_int*height_int

def calculateCircleArea():
____print "Program to calculate Area of Circle"
____radius = raw_input("Please enter radius -> ")
____radius_int = int(radius)
____print "Circle area of radius", radius , "is",3.14159*(radius_int**2)

def calculateTrapezoidsArea():
____print "Program to calculate Area of Trapezoids"
____parallel_base1 = raw_input("Please enter parallel base1 -> ")
____parallel_base2 = raw_input("Please enter parallel base2 -> ")
____height = raw_input("Please enter height -> ")
____parallel_base_int1 = int(parallel_base1)
____parallel_base_int2 = int(parallel_base2)
____height_int = int(height)
____print "Trapezoids area of parallel_base1", parallel_base1,"Trapezoids area of\
__________parallel_base2", parallel_base2, "and height", height, "is",\
__________float(0.5*parallel_base_int1+parallel_base_int2*height_int)

def xxx():
____print "Please type R,T or C to choose the type of area to calculate"
choice = raw_input("Which type of area you would like to calculate (R/T/C/Z)? -> ")
if choice == "R" or choice == "r":
____calculateRectangleArea()
if choice == "T" or choice == "t":
____calculateTriangleArea()
if choice == "C" or choice == "c":
____calculateCircleArea()
if choice == "Z" or choice == "z":
____calculateTrapezoidsArea()
if choice != "R" and choice != "r" and choice != "T" and choice != "t" and\
___choice != "C" and choice != "c" and choice != "Z" and choice != "z" :
____xxx()

แบบที่ 6

def calculateRectangleArea():
____print "Program to calculate Area of Rectangle"
____base = raw_input("Please enter base -> ")
____height = raw_input("Please enter height -> ")
____base_int = int(base)
____height_int = int(height)
____print "Rectangle area of base", base, "and height", height, "is",\
__________base_int*height_int

def calculateTriangleArea():
____print "Program to calculate Area of Triangle"
____base = raw_input("Please enter base -> ")
____height = raw_input("Please enter height -> ")
____base_int = int(base)
____height_int = int(height)
____print "Triangle area of base", base, "and height", height, "is",\
__________base_int*height_int*0.5

def calculateCircleArea():
____print "Program to calculate Area of Circle"
____radious = raw_input("Please enter radious -> ")
____radious_int = int(radious)
____print "Circle area of radious", radious, "is", 3.14159*radious_int*radious_int

def calculateTrapezoidArea():
____print "Program to calculate Area of TrapeZoid"
____parallel_line1 = raw_input("Please enter length of 1st parallel line -> ")
____parallel_line2 = raw_input("Please enter length of 2nd parallel line -> ")
____height = raw_input("Please enter height -> ")
____parallel_line1_int = int(parallel_line1)
____parallel_line2_int = int(parallel_line2)
____height_int = int(height)
____print "Trapezoid area of 1st parallel line", parallel_line1, ", 2nd parallel\
__________line", parallel_line2, "and height", height, "is",\
__________(parallel_line1_int+parallel_line2_int)*height_int*0.5

i = 0
while i == 0:
____choice = raw_input("Which type of area you would like to\
________________________calculate (R,T,Z or C)? -> ")
________if choice == "R" or choice == "r":
____________calculateRectangleArea()
____________i += 1
________if choice == "T" or choice == "t":
____________calculateTriangleArea()
____________i += 1
________if choice == "C" or choice == "c":
____________calculateCircleArea()
____________i += 1
________if choice == "Z" or choice == "z":
____________calculateTrapezoidArea()
____________i += 1
________if choice != "R" and choice != "r" and choice != "T" and choice != "t"\
___________and choice != "C" and choice != "c" and choice != "Z" and choice != "z":
____________print "Please type R,T or C to choose the type of area to calculate."

If-Elif-Else structure

จากตัวอย่างครั้งที่แล้ว เราจะเห็นโครงสร้างในส่วนของ if มากขึ้น

choice = raw_input("Which type of area you would like to calculate (R/T/C/Z)? -> ")
if choice == "R" or choice == "r":
____calculateRectangleArea()
if choice == "T" or choice == "t":
____calculateTriangleArea()
if choice == "C" or choice == "c":
____calculateCircleArea()
if choice == "Z" or choice == "z":
____calculateTrapezoidsArea()
if choice != "R" and choice != "r" and choice != "T" and choice != "t" and \
___choice != "C" and choice != "c" and choice != "Z" and choice != "z":
____print "Please type R,T or C to choose the type of area to calculate"

สิ่งที่น่าสนใจใน code ข้างบน คือ การใช้ backslash (\) ในการเชื่อมบรรทัดที่ค่อนข้างยาว ทำให้อ่าน code ได้ง่ายขึ้น อันนี้เป็น trick หนึ่งที่น่าสนใจและเป็นประโยชน์

เมื่อเราดึงส่วนของโปรแกรมที่เป็นรายละเอียดออกไปเป็นฟังก์ชันแล้ว เราจะเห็นว่าทุกครั้งที่เราเพิ่มตัวเลือกเข้าไป เราจำเป็นจะต้องแก้ไข code 2 ที่ คือ

1.ในส่วนที่จะเรียกใช้ฟังก์ชันที่จำเพาะสำหรับตัวเลือกนั้น ๆ และไปเพิ่มในหัวข้อ if สุดท้าย

ถ้าหากเรามีตัวเลือกเยอะๆ คำสั่ง if ที่บรรทัดสุดท้ายคงจะยาวมากๆ แต่เราสามารถแก้ไขปัญหานี้ด้วย รูปแบบ if-elif-else ดังนี้

if choice == "R" or choice == "r":
____calculateREctangleArea()
elif choice == "T" or choice == "t":
____calculateTriangleArea()
.
.
.
else:
____print "Please type R, T, C or Z to choose the type of area to calculate"

ด้วยคำสั่งแบบนี้ เมื่อโปรแกรมตรวจสอบเงื่อนไขแรกแล้ว ไม่ถูกต้อง ก็จะมาทำงานที่เงื่อนไขถัดไปใน elif ไปเรื่อยๆ จนกระทั่งถึง elif ตัวสุดท้าย ถ้าหากเงื่อนไขยังคงไม่ถูกต้อง โปรแกรมก็จะมาทำงานในส่วนที่อยู่ใน else block เพราะฉะนั้น เราไม่ต้องใส่เงื่อนไขยาวๆ ที่ if คำสั่งสุดท้ายอีกต่อไป เพราะว่าเมื่อโปรแกรมหาคำสั่งที่เหมาะสมเจอ (if condition เป็นจริง) ก็จะไปทำงานตามที่ต้องการ และกระโดดไปทำงานที่คำสั่งถัดไปที่อยู่นอกโครงสร้าง if-elif-else เลย แต่ถ้าหากคำสั่งที่เหมาะสมไม่เจอ ก็จะไปทำงานที่ส่วน else

แบบฝึกหัด

1. นำโครงสร้าง if-elif-else มาใช้ในโปรแกรม
2. นำ backslash (\) มาช่วยแบ่งบรรทัด ในกรณีที่คำสั่งในบรรทัดนั้นยาวเกินไป

1 comment:

  1. มาแล้วครับ สำหรับคนที่ต้องการจะ download นิตยสารทั้งเล่ม ในรูปแบบ PDF ขอเชิญได้ที่นี่เลยครับ

    http://www.4shared.com/file/236278340/4b3a94e6/THAI_Bioinformatics-February_2.html

    ReplyDelete