วันจันทร์ที่ 26 ตุลาคม พ.ศ. 2558

LAB 7 Generic

LAB 7 Generic 1 ( Display each student record )
class Student:
    def __init__(self,name,idNumber,age,weight,height):
        self.name = name
        self.idNumber = idNumber
        self.age = age
        self.weight = weight
        self.height = height      
    def display_name(self):
        return self.name
    def display_idNumber(self):  
        return self.idNumber    
    def display_age(self):
        return self.age
    def display_weight(self):
        return self.weight
    def display_height(self):    
        return self.height
 
def display_studen_data(StuRecode):
    index = 0
    print("--Students Record--")
    while(index < len(StuRecode)):
        print("---",index+1,"---")
        print("Name : ",StuRecode[index].display_name())
        print("ID : ",StuRecode[index].display_idNumber())
        print("Age : ",StuRecode[index].display_age()," years")
        print("Weight : ",StuRecode[index].display_weight()," Kg")
        print("Height : ",StuRecode[index].display_height()," cm")
        print("")
        index += 1  
 
def setup():
    a = Student( "Andy" , 44592 , 19 , 55 , 168 )
    b = Student( "Mike" , 44593 , 32 , 65 , 173 )
    c = Student( "Nick" , 44594 , 27 , 61 , 165 )
    d = Student( "Jack" , 44595 , 35 , 68 , 158)
    e = Student( "Lucy" , 44596 , 55 , 75 , 175)
    f = Student( "Max" , 44597 , 18 , 56 , 163 )
    g = Student( "Luck" , 44598 , 20 , 58 , 169 )
    StuRecode = [a,b,c,d,e,f,g]
    display_studen_data(StuRecode)
 
setup()  

LAB 7 Generic 2 ( Find student BMI )
class Student:
    def __init__(self,name,idNumber,age,weight,height):
        self.name = name
        self.idNumber = idNumber
        self.age = age
        self.weight = weight
        self.height = height    
    def display_name(self):
        return self.name
    def display_idNumber(self):
        return self.idNumber  
    def display_age(self):
        return self.age
    def display_weight(self):
        return self.weight
    def display_height(self):  
        return self.height
     
def cal_bmi(StuRecode,idNum):
    index = idNum-44592
    print("-----------------")
    if(index >= 0 and index <= 6):
        bmi = StuRecode[index].display_weight()/(pow(StuRecode[index].display_height()/100,2))
        print("Student name : ",StuRecode[index].display_name())
        print("ID : ",StuRecode[index].display_idNumber())
        print("weight : ",StuRecode[index].display_weight()," Kg",end =" ")
        print("Height : ",StuRecode[index].display_height()," cm")
        print("BMI : ",bmi)
    else:
        print("*No data*")      
 
def setup():
    a = Student( "Andy" , 44592 , 19 , 55 , 168 )
    b = Student( "Mike" , 44593 , 32 , 65 , 173 )
    c = Student( "Nick" , 44594 , 27 , 61 , 165 )
    d = Student( "Jack" , 44595 , 35 , 68 , 158)
    e = Student( "Lucy" , 44596 , 55 , 75 , 175)
    f = Student( "Max" , 44597 , 18 , 56 , 163 )
    g = Student( "Luck" , 44598 , 20 , 58 , 169 )
    StuRecode = [a,b,c,d,e,f,g]
    idNum = int(input("ID Number : "))
    cal_bmi(StuRecode,idNum)
 
setup()

LAB 7 Generic 3 ( Find/count number of students with BMI > 25 ) 
class Student:
    def __init__(self,name,idNumber,age,weight,height):
        self.name = name
        self.idNumber = idNumber
        self.age = age
        self.weight = weight
        self.height = height    
    def display_name(self):
        return self.name
    def display_idNumber(self):
        return self.idNumber  
    def display_age(self):
        return self.age
    def display_weight(self):
        return self.weight
    def display_height(self):  
        return self.height
     
def count_bmi_above_25(StuRecode):
    index = 0
    count = 0
    while(index < len(StuRecode)):
        weight = StuRecode[index].display_weight()
        height = StuRecode[index].display_height()
        bmi =  weight/(pow(height/100,2))
        if(bmi > 25):
            count += 1
        index += 1
    print("Number of students with BMI > 25 is ",count)        
 
def setup():
    a = Student( "Andy" , 44592 , 19 , 55 , 168 )
    b = Student( "Mike" , 44593 , 32 , 65 , 173 )
    c = Student( "Nick" , 44594 , 27 , 61 , 165 )
    d = Student( "Jack" , 44595 , 35 , 68 , 158)
    e = Student( "Lucy" , 44596 , 55 , 75 , 175)
    f = Student( "Max" , 44597 , 18 , 56 , 163 )
    g = Student( "Luck" , 44598 , 20 , 58 , 169 )
    StuRecode = [a,b,c,d,e,f,g]
    count_bmi_above_25(StuRecode)
 
setup()

LAB 7 Generic 4 ( Display student records with BMI > 25 )
class Student:
    def __init__(self,name,idNumber,age,weight,height):
        self.name = name
        self.idNumber = idNumber
        self.age = age
        self.weight = weight
        self.height = height    
    def display_name(self):
        return self.name
    def display_idNumber(self):
        return self.idNumber  
    def display_age(self):
        return self.age
    def display_weight(self):
        return self.weight
    def display_height(self):  
        return self.height
     
def Display_bmi_above_25(StuRecode):
    index = 0
    count = 0
    while(index < len(StuRecode)):
        bmi = StuRecode[index].display_weight()/(pow(StuRecode[index].display_height()/100,2))
        if(bmi > 25):
            count += 1
            print("----",count,"----")
            print("Student name : ",StuRecode[index].display_name())
            print("ID : ",StuRecode[index].display_idNumber())
            print("weight : ",StuRecode[index].display_weight()," Kg",end =" ")
            print("Height : ",StuRecode[index].display_height()," cm")
            print("BMI : ",bmi)
            print("")
        index += 1
    print("Number of students with BMI > 25 is ",count)    
 
def setup():
    a = Student( "Andy" , 44592 , 19 , 55 , 168 )
    b = Student( "Mike" , 44593 , 32 , 65 , 173 )
    c = Student( "Nick" , 44594 , 27 , 61 , 165 )
    d = Student( "Jack" , 44595 , 35 , 68 , 158)
    e = Student( "Lucy" , 44596 , 55 , 75 , 175)
    f = Student( "Max" , 44597 , 18 , 56 , 163 )
    g = Student( "Luck" , 44598 , 20 , 58 , 169 )
    StuRecode = [a,b,c,d,e,f,g]
    Display_bmi_above_25(StuRecode)
 
setup()

LAB 7 Generic 5 ( Find average age of students )
class Student:
    def __init__(self,name,idNumber,age,weight,height):
        self.name = name
        self.idNumber = idNumber
        self.age = age
        self.weight = weight
        self.height = height    
    def display_name(self):
        return self.name
    def display_idNumber(self):
        return self.idNumber  
    def display_age(self):
        return self.age
    def display_weight(self):
        return self.weight
    def display_height(self):  
        return self.height
     
def find_avg_age(StuRecode):
    index = 0
    avg = 0
    while(index < len(StuRecode)):
        avg += StuRecode[index].display_age()
        index += 1
    avg /= len(StuRecode)
    avg *= 100
    avg = round(avg)
    avg /= 100
    print("Average age of students :",avg," years")    
 
def setup():
    a = Student( "Andy" , 44592 , 19 , 55 , 168 )
    b = Student( "Mike" , 44593 , 32 , 65 , 173 )
    c = Student( "Nick" , 44594 , 27 , 61 , 165 )
    d = Student( "Jack" , 44595 , 35 , 68 , 158)
    e = Student( "Lucy" , 44596 , 55 , 75 , 175)
    f = Student( "Max" , 44597 , 18 , 56 , 163 )
    g = Student( "Luck" , 44598 , 20 , 58 , 169 )
    StuRecode = [a,b,c,d,e,f,g]
    find_avg_age(StuRecode)
 
setup()

LAB 7 Generic 6 ( Find/count number of students with age < 30 )
class Student:
    def __init__(self,name,idNumber,age,weight,height):
        self.name = name
        self.idNumber = idNumber
        self.age = age
        self.weight = weight
        self.height = height    
    def display_name(self):
        return self.name
    def display_idNumber(self):
        return self.idNumber  
    def display_age(self):
        return self.age
    def display_weight(self):
        return self.weight
    def display_height(self):  
        return self.height
     
def count_below_30_year(StuRecode):
    index = 0
    count = 0
    while(index < len(StuRecode)):
        if(StuRecode[index].display_age() < 30):
            count += 1
        index += 1
    print("Number of students with age < 30 is ",count)    
 
def setup():
    a = Student( "Andy" , 44592 , 19 , 55 , 168 )
    b = Student( "Mike" , 44593 , 32 , 65 , 173 )
    c = Student( "Nick" , 44594 , 27 , 61 , 165 )
    d = Student( "Jack" , 44595 , 35 , 68 , 158)
    e = Student( "Lucy" , 44596 , 55 , 75 , 175)
    f = Student( "Max" , 44597 , 18 , 56 , 163 )
    g = Student( "Luck" , 44598 , 20 , 58 , 169 )
    StuRecode = [a,b,c,d,e,f,g]
    count_below_30_year(StuRecode)
 
setup()

LAB 7 Generic 7 ( Display student records, sorted by age, use insertion sort )
class Student:
    def __init__(self,name,idNumber,age,weight,height):
        self.name = name
        self.idNumber = idNumber
        self.age = age
        self.weight = weight
        self.height = height    
    def display_name(self):
        return self.name
    def display_idNumber(self):
        return self.idNumber  
    def display_age(self):
        return self.age
    def display_weight(self):
        return self.weight
    def display_height(self):  
        return self.height
     
def display_data_sort_by_age(StuRecode):
    index1 = 0
    temp = [None]*len(StuRecode)
    while(index1 < len(StuRecode)):
        temp[index1] = StuRecode[index1].display_age()
        index1 += 1
    index1 = 0  
    while(index1 < len(temp)):
        index2 = index1 + 1
        while(index2 < len(temp)):
            if(temp[index1] > temp[index2]):
                temp2 = temp[index1]
                temp[index1] = temp[index2]
                temp[index2] = temp2
            index2 += 1
        index1 += 1
    index1 = 0
    print("---Sorted by age---")
    count = 0
    while(index1 < len(StuRecode)):
        index2 = 0
        stop = False
        while(index2 < len(temp) and (not stop)):
            if(temp[index1] == StuRecode[index2].display_age()):
                print("---",count+1,"---")
                print("Student name : ",StuRecode[index2].display_name() )
                print("ID : ",StuRecode[index2].display_idNumber() )
                print("Age : ",StuRecode[index2].display_age()," years")
                print("weight : ",StuRecode[index2].display_weight()," Kg")
                print("Height : ",StuRecode[index2].display_height()," cm")
                print("")
                stop = True
                count += 1
            index2 += 1
        index1 += 1
 
def setup():
    a = Student( "Andy" , 44592 , 19 , 55 , 168 )
    b = Student( "Mike" , 44593 , 32 , 65 , 173 )
    c = Student( "Nick" , 44594 , 27 , 61 , 165 )
    d = Student( "Jack" , 44595 , 35 , 68 , 158)
    e = Student( "Lucy" , 44596 , 55 , 75 , 175)
    f = Student( "Max" , 44597 , 18 , 56 , 163 )
    g = Student( "Luck" , 44598 , 20 , 58 , 169 )
    StuRecode = [a,b,c,d,e,f,g]
    display_data_sort_by_age(StuRecode)
 
setup()

LAB 7 Generic 8 ( Find minimum weight of students )
class Student:
    def __init__(self,name,idNumber,age,weight,height):
        self.name = name
        self.idNumber = idNumber
        self.age = age
        self.weight = weight
        self.height = height    
    def display_name(self):
        return self.name
    def display_idNumber(self):
        return self.idNumber  
    def display_age(self):
        return self.age
    def display_weight(self):
        return self.weight
    def display_height(self):  
        return self.height
     
def find_minimum_weight(StuRecode):
    index = 0
    ref_value = StuRecode[index].display_weight()
    while(index < len(StuRecode)):
        if(ref_value > StuRecode[index].display_weight()):
            ref_value = StuRecode[index].display_weight()
        index += 1
    print("Minimum weight of students is ",ref_value)
 
def setup():
    a = Student( "Andy" , 44592 , 19 , 55 , 168 )
    b = Student( "Mike" , 44593 , 32 , 65 , 173 )
    c = Student( "Nick" , 44594 , 27 , 61 , 165 )
    d = Student( "Jack" , 44595 , 35 , 68 , 158)
    e = Student( "Lucy" , 44596 , 55 , 75 , 175)
    f = Student( "Max" , 44597 , 18 , 56 , 163 )
    g = Student( "Luck" , 44598 , 20 , 58 , 169 )
    StuRecode = [a,b,c,d,e,f,g]
    find_minimum_weight(StuRecode)
 
setup()

LAB 7 Generic 9 ( Find/count number of students with weight < 50 )
class Student:
    def __init__(self,name,idNumber,age,weight,height):
        self.name = name
        self.idNumber = idNumber
        self.age = age
        self.weight = weight
        self.height = height    
    def display_name(self):
        return self.name
    def display_idNumber(self):
        return self.idNumber  
    def display_age(self):
        return self.age
    def display_weight(self):
        return self.weight
    def display_height(self):  
        return self.height
     
def count_below_weight_50(StuRecode):
    index = 0
    count = 0
    while(index < len(StuRecode)):
        if(50 > StuRecode[index].display_weight()):
            count += 1
        index += 1
    print("Number of students with weight < 50 is ",count)
 
def setup():
    a = Student( "Andy" , 44592 , 19 , 55 , 168 )
    b = Student( "Mike" , 44593 , 32 , 65 , 173 )
    c = Student( "Nick" , 44594 , 27 , 61 , 165 )
    d = Student( "Jack" , 44595 , 35 , 68 , 158)
    e = Student( "Lucy" , 44596 , 55 , 75 , 175)
    f = Student( "Max" , 44597 , 18 , 56 , 163 )
    g = Student( "Luck" , 44598 , 20 , 58 , 169 )
    StuRecode = [a,b,c,d,e,f,g]
    count_below_weight_50(StuRecode)
 

setup()

LAB 7 Generic 10 ( Display student records with weight < 50 )
class Student:
    def __init__(self,name,idNumber,age,weight,height):
        self.name = name
        self.idNumber = idNumber
        self.age = age
        self.weight = weight
        self.height = height    
    def display_name(self):
        return self.name
    def display_idNumber(self):
        return self.idNumber  
    def display_age(self):
        return self.age
    def display_weight(self):
        return self.weight
    def display_height(self):  
        return self.height
     
def display_weight_below_50_data(StuRecode):
    index = 0
    print("--Student records with weight < 50--")
    while(index < len(StuRecode)):
        if(50 > StuRecode[index].display_weight() ):
            print("---",index+1,"---")
            print("Student name : ",StuRecode[index].display_name())
            print("ID : ",StuRecode[index].display_idNumber())
            print("Age : ",StuRecode[index].display_age()," years")
            print("weight : ",StuRecode[index].display_weight()," Kg")
            print("Height : ",StuRecode[index].display_height()," cm")
            print("")
        index += 1
 
def setup():
    a = Student( "Mabel" , 44592 , 19 , 49 , 163 )
    b = Student( "Mike" , 44593 , 32 , 65 , 173 )
    c = Student( "Nick" , 44594 , 27 , 61 , 165 )
    d = Student( "Jack" , 44595 , 35 , 68 , 158)
    e = Student( "Lucy" , 44596 , 55 , 75 , 175)
    f = Student( "Max" , 44597 , 18 , 56 , 163 )
    g = Student( "Luck" , 44598 , 20 , 58 , 169 )
    StuRecode = [a,b,c,d,e,f,g]
    display_weight_below_50_data(StuRecode)
 

setup()

LAB 7 Generic 11 ( Create class Banner for printing a banner 1 )
class Alphabet:
    def __init__(self,N_line1,N_line2,N_line3,N_line4,N_line5):
        self.N_line1 = N_line1
        self.N_line2 = N_line2
        self.N_line3 = N_line3
        self.N_line4 = N_line4
        self.N_line5 = N_line5
    def display_alphabet_L1(self):
        print(self.N_line1,end = "")
    def display_alphabet_L2(self):
        print(self.N_line2,end = "")
    def display_alphabet_L3(self):
        print(self.N_line3,end = "")
    def display_alphabet_L4(self):
        print(self.N_line4,end = "")
    def display_alphabet_L5(self):
        print(self.N_line5,end = "")

def draw_alphabet(alphabet_group,word):      
    index_print = 0
    while(index_print < 5):
        index_word = 0
        while(index_word < len(word)):
            if(word[index_word] == "N"):
                print_func(alphabet_group,index_print,0)
            elif(word[index_word] == "O"):
                print_func(alphabet_group,index_print,1)
            elif(word[index_word] == "B"):
                print_func(alphabet_group,index_print,2)
            index_word += 1
        print()
        index_print += 1

def print_func(alphabet_group,index_print,alphabet_index):
    if(index_print == 0):
        alphabet_group[alphabet_index].display_alphabet_L1()
    elif(index_print == 1):  
        alphabet_group[alphabet_index].display_alphabet_L2()
    elif(index_print == 2):  
        alphabet_group[alphabet_index].display_alphabet_L3()
    elif(index_print == 3):  
        alphabet_group[alphabet_index].display_alphabet_L4()
    elif(index_print == 4):  
        alphabet_group[alphabet_index].display_alphabet_L5()  

def setup():
    N = Alphabet(" #    # ",
                 " ##   # ",
                 " # #  # ",
                 " #  # # ",
                 " #   ## " )
    O = Alphabet("  ####  ",
                 " #    # ",
                 " #    # ",
                 " #    # ",
                 "  ####  " )
    B = Alphabet(" #####  ",
                 " #    # ",
                 " #####  ",
                 " #    # ",
                 " #####  " )
    alphabet_group = [N,O,B]
    word = input("Draw : ")
    draw_alphabet(alphabet_group,word)
                                   

setup()

LAB 7 Generic 11 ( Create class Banner for printing a banner 2 )
class Alphabet:
    def __init__(self,N_line1,N_line2,N_line3,N_line4,N_line5):
        self.N_line1 = N_line1
        self.N_line2 = N_line2
        self.N_line3 = N_line3
        self.N_line4 = N_line4
        self.N_line5 = N_line5
    def display_alphabet_L1(self):
        print(self.N_line1,end = "")
    def display_alphabet_L2(self):
        print(self.N_line2,end = "")
    def display_alphabet_L3(self):
        print(self.N_line3,end = "")
    def display_alphabet_L4(self):
        print(self.N_line4,end = "")
    def display_alphabet_L5(self):
        print(self.N_line5,end = "")
    def get_alphabet_L1(self):
        return self.N_line1
    def get_alphabet_L2(self):
        return self.N_line2  
    def get_alphabet_L3(self):
        return self.N_line3
    def get_alphabet_L4(self):
        return self.N_line4
    def get_alphabet_L5(self):
        return self.N_line5
    def print_star_L1(self):
        hastag_to_star(self.N_line1)
    def print_star_L2(self):
        hastag_to_star(self.N_line2)
    def print_star_L3(self):
        hastag_to_star(self.N_line3)
    def print_star_L4(self):
        hastag_to_star(self.N_line4)
    def print_star_L5(self):  
        hastag_to_star(self.N_line5)
       
def hastag_to_star(temp):
    index = 0
    while(index < len(temp)):
        if(temp[index]  == "#"):
            print("*",end = "")
        else:
            print(" ",end = "")
        index += 1

def draw_alphabet(alphabet_group,word):    
    index_print = 0
    while(index_print < 5):
        index_word = 0
        while(index_word < len(word)):
            if(word[index_word] == "N"):
                print_func(alphabet_group,index_print,0)
            elif(word[index_word] == "O"):
                print_func(alphabet_group,index_print,1)
            elif(word[index_word] == "B"):
                print_func(alphabet_group,index_print,2)
            index_word += 1
        print()
        index_print += 1

def print_func(alphabet_group,index_print,alphabet_index):
    if(index_print == 0):
        alphabet_group[alphabet_index].print_star_L1()
    elif(index_print == 1):
        alphabet_group[alphabet_index].print_star_L2()
    elif(index_print == 2):
        alphabet_group[alphabet_index].print_star_L3()
    elif(index_print == 3):
        alphabet_group[alphabet_index].print_star_L4()
    elif(index_print == 4):
        alphabet_group[alphabet_index].print_star_L5()

def setup():
    N = Alphabet(" #    # ",
                 " ##   # ",
                 " # #  # ",
                 " #  # # ",
                 " #   ## " )
    O = Alphabet("  ####  ",
                 " #    # ",
                 " #    # ",
                 " #    # ",
                 "  ####  " )
    B = Alphabet(" #####  ",
                 " #    # ",
                 " #####  ",
                 " #    # ",
                 " #####  " )
    alphabet_group = [N,O,B]
    word = input("Draw : ")
    draw_alphabet(alphabet_group,word)
                                 


setup()

วันจันทร์ที่ 19 ตุลาคม พ.ศ. 2558

LAB 6 Generic 1-19


LAB 6 Generic 1( Display student records )
def setup():
    name = ["Andy","Mike","Nick","Jack","Lucy","Max","Luck"]
    idNumber = [44592,44593,44594,44595,44596,44597,44598]
    age = [19,32,27,35,55,18,20]
    weight = [55,65,61,68,75,56,58]
    height = [168,173,165,158,175,163,169]
    display_studen_data(name,idNumber,age,weight,height)
 
def display_studen_data(name,idNumber,age,weight,height):  
    index = 0
    print("--Students Record--")
    while(index < len(name)):
        print("---",index+1,"---")
        print("Name : ",name[index])
        print("ID : ",idNumber[index])
        print("Age : ",age[index]," years")
        print("Weight : ",weight[index]," Kg")
        print("Height : ",height[index]," cm")
        print("")
        index += 1
     
setup()      

LAB 6 Generic 2( Find student BMI )
def setup():
    name = ["Andy","Mike","Nick","Jack","Lucy","Max","Luck"]
    idNumber = [44592,44593,44594,44595,44596,44597,44598]
    age = [19,32,27,35,55,18,20]
    weight = [55,65,61,68,75,56,58]
    height = [168,173,165,158,175,163,169]
    idNum = int(input("ID number : "))
    cal_bmi(name,idNumber,weight,height,idNum)
 
def cal_bmi(name,idNumber,weight,height,idNum):  
    index = idNum-44592
    print("-----------------")
    if(index >= 0 and index <= 6):
        bmi = weight[index]/(pow(height[index]/100,2))
        print("Student name : ",name[index])
        print("ID : ",idNumber[index])
        print("weight : ",weight[index]," Kg",end ="")
        print("Height : ",height[index]," cm")
        print("BMI : ",bmi)
    else:
        print("*No data*")
     
setup()  

LAB 6 Generic 3( Find/count number of students with BMI > 25 )
def setup():
    name = ["Andy","Mike","Nick","Jack","Lucy","Max","Luck"]
    idNumber = [44592,44593,44594,44595,44596,44597,44598]
    age = [19,32,27,35,55,18,20]
    weight = [55,65,61,68,75,56,58]
    height = [168,173,165,158,175,163,169]
    count_bmi_above_25(weight,height)
 
def count_bmi_above_25(weight,height):  
    index = 0
    count = 0
    while(index < len(weight)):
        bmi = weight[index]/(pow(height[index]/100,2))
        if(bmi > 25):
            count += 1
        index += 1  
    print("Number of students with BMI > 25 is ",count)      
     

setup()

LAB 6 Generic 4( Display student records with BMI > 25  )
def setup():
    name = ["Andy","Mike","Nick","Jack","Lucy","Max","Luck"]
    idNumber = [44592,44593,44594,44595,44596,44597,44598]
    age = [19,32,27,35,55,18,20]
    weight = [55,65,61,68,75,56,58]
    height = [168,173,165,158,175,163,169]
    Display_bmi_above_25(name,idNumber,age,weight,height)
 
def Display_bmi_above_25(name,idNumber,age,weight,height):  
    index = 0
    count = 0
    while(index < len(weight)):
        bmi = weight[index]/(pow(height[index]/100,2))
        if(bmi > 25):
            count += 1
            print("Student name : ",name[index])
            print("ID : ",idNumber[index])
            print("weight : ",weight[index]," Kg",end ="")
            print("Height : ",height[index]," cm")
            print("BMI : ",bmi)
            print("")
        index += 1  
    print("Number of students with BMI > 25 is ",count)      
     

setup()

LAB 6 Generic 5( Find average age of students  )
def setup():
    name = ["Andy","Mike","Nick","Jack","Lucy","Max","Luck"]
    idNumber = [44592,44593,44594,44595,44596,44597,44598]
    age = [19,32,27,35,55,18,20]
    weight = [55,65,61,68,75,56,58]
    height = [168,173,165,158,175,163,169]
    find_avg_age(age)
 
def find_avg_age(age):  
    index = 0
    avg = 0
    while(index < len(age)):
        avg += age[index]
        index += 1  
    avg /= len(age)
    avg *= 100
    avg = round(avg)
    avg /= 100
    print("Average age of students :",avg," years")  
     
setup()

LAB 6 Generic 6( Find/count number of students with age < 30 )
def setup():
    name = ["Andy","Mike","Nick","Jack","Lucy","Max","Luck"]
    idNumber = [44592,44593,44594,44595,44596,44597,44598]
    age = [19,32,27,35,55,18,20]
    weight = [55,65,61,68,75,56,58]
    height = [168,173,165,158,175,163,169]
    count_below_30_year(age)
 
def count_below_30_year(age):  
    index = 0
    count = 0
    while(index < len(age)):
        if(age[index] < 30):
            count += 1
        index += 1  
    print("Number of students with age < 30 is ",count)  
     

setup()

LAB 6 Generic 7( Display student records, sorted by age )
def setup():
    name = ["Andy","Mike","Nick","Jack","Lucy","Max","Luck"]
    idNumber = [44592,44593,44594,44595,44596,44597,44598]
    age = [32,32,27,35,55,18,20]
    weight = [55,65,61,68,75,56,58]
    height = [168,173,165,158,175,163,169]
    display_data_sort_by_age(name,idNumber,age,weight,height)

def display_data_sort_by_age(name,idNumber,age,weight,height):
    index1 = 0
    temp = age
    while(index1 < len(temp)):
        index2 = index1 + 1
        while(index2 < len(temp)):
            if(temp[index1] > temp[index2]):
                temp2 = temp[index1]
                temp[index1] = temp[index2]
                temp[index2] = temp2
            index2 += 1
        index1 += 1
    index1 = 0
    print("---Sorted by age---")
    count = 0
    while(index1 < len(age)):
        index2 = 0
        stop = False
        while(index2 < len(temp) and (not stop)):
            if(temp[index2] == age[index1]):
                print("---",count+1,"---")
                print("Student name : ",name[index1])
                print("ID : ",idNumber[index1])
                print("Age : ",age[index1]," years")
                print("weight : ",weight[index1]," Kg")
                print("Height : ",height[index1]," cm")
                print("")
                stop = True
                count += 1
            index2 += 1
        index1 += 1

setup()

LAB 6 Generic 8( Find minimum weight of students )
def setup():
    name = ["Andy","Mike","Nick","Jack","Lucy","Max","Luck"]
    idNumber = [44592,44593,44594,44595,44596,44597,44598]
    age = [19,32,27,35,55,18,20]
    weight = [55,65,61,68,75,56,58]
    height = [168,173,165,158,175,163,169]
    find_minimum_weight(weight)

def find_minimum_weight(weight):
    index = 0
    ref_value = weight[0]
    while(index < len(weight)):
        if(ref_value > weight[index]):
            ref_value = weight[index]
        index += 1
    print("Minimum weight of students is ",ref_value)
   


setup()

LAB 6 Generic 9( Number of students with weight < 50 )
def setup():
    name = ["Andy","Mike","Nick","Jack","Lucy","Max","Luck"]
    idNumber = [44592,44593,44594,44595,44596,44597,44598]
    age = [19,32,27,35,55,18,20]
    weight = [55,65,61,68,75,56,58]
    height = [168,173,165,158,175,163,169]
    find_minimum_weight(weight)

def find_minimum_weight(weight):
    index = 0
    count = 0
    while(index < len(weight)):
        if(50 > weight[index]):
            count += 1
        index += 1
    print("Number of students with weight < 50 is ",count)
   


setup()

LAB 6 Generic 10( Display student records with weight < 50 )
def setup():
    name = ["Mabel","Mike","Nick","Jack","Lucy","Max","Luck"]
    idNumber = [44592,44593,44594,44595,44596,44597,44598]
    age = [19,32,27,35,55,18,20]
    weight = [45,65,61,68,75,56,58]
    height = [155,173,165,158,175,163,169]
    display_weight_below_50_data(name,idNumber,age,weight,height)

def display_weight_below_50_data(name,idNumber,age,weight,height):
    index = 0
    print("--Student records with weight < 50--")
    while(index < len(weight)):
        if(50 > weight[index]):
            print("---",index+1,"---")
            print("Student name : ",name[index])
            print("ID : ",idNumber[index])
            print("Age : ",age[index]," years")
            print("weight : ",weight[index]," Kg")
            print("Height : ",height[index]," cm")
            print("")
        index += 1


setup()

LAB 6 Generic 11( Find total number of chairs (in the building) )
def setup():
    floor1 = [53,55,54,50,57,55,52]
    floor2 = [49,53,56,54,57,58,56]
    floor3 = [53,52,60,50,51,49,59]
    floor4 = [55,56,54,49,52,57,53]
    floor5 = [48,46,52,58,57,53,54]
    building = [floor1,floor2,floor3,floor4,floor5]
    find_total_chairs(building)

def find_total_chairs(building):
    index_floor = 0
    total = 0
    while(index_floor < len(building)):
        index_room = 0
        while(index_room < len(building[index_floor])):
            total += building[index_floor][index_room]
            index_room += 1
        index_floor += 1
    print("Total number of chairs (in the building) is ",total)  

setup()

LAB 6 Generic 12( Find index of the floor(s) with maximum number of chairs (in the building) )
def setup():
    floor1 = [53,55,54,50,57,55,52]
    floor2 = [49,53,56,54,57,58,56]
    floor3 = [53,52,60,50,51,49,59]
    floor4 = [55,56,54,49,52,57,53]
    floor5 = [48,46,52,58,57,53,54]
    building = [floor1,floor2,floor3,floor4,floor5]
    find_total_chairs(building)

def find_total_chairs(building):
    index_floor = 0
    total_in_floor = 0
    ref_value = 0
    while(index_floor < len(building)):
        index_room = 0
        while(index_room < len(building[index_floor])):
            total_in_floor += building[index_floor][index_room]
            index_room += 1
        if(total_in_floor > ref_value):
            ref_value = total_in_floor
            index_max_floor = index_floor
        index_floor += 1
    print("Index of floor with maximum chairs is ",index_max_floor)
    print("There are ",ref_value," chairs in that room")


setup()

LAB 6 Generic 13( Find indices of the room(s) with maximum number of chairs (in the building) )
def setup():
    floor1 = [53,55,54,50,57,55,52]
    floor2 = [49,53,56,54,57,58,56]
    floor3 = [53,52,59,50,51,49,59]
    floor4 = [55,56,54,49,52,57,53]
    floor5 = [48,46,52,58,57,53,54]
    building = [floor1,floor2,floor3,floor4,floor5]
    find_total_chairs(building)

def find_total_chairs(building):
    index_floor = 0
    ref_value = building[0][0]
    while(index_floor < len(building)):
        index_room = 0
        while(index_room < len(building[index_floor])):
            if(building[index_floor][index_room] > ref_value):
                ref_value = building[index_floor][index_room]              
            index_room += 1
        index_floor += 1
    index_floor = 0  
    print("Indices of rooms with maximum  chairs :",end = "")
    while(index_floor < len(building)):
        index_room = 0
        while(index_room < len(building[index_floor])):
           if( ref_value == building[index_floor][index_room]):
               print(" [",index_floor,"][",index_room,"] ",end = "")
           index_room += 1
        index_floor += 1  


setup()

LAB 6 Generic 14( Display the matrix )
def setup():
    row1 = [2,5,4]
    row2 = [9,5,6]
    row3 = [3,2,5]
    matrix1 = [row1,row2,row3]
    row4 = [5,6,4]
    row5 = [8,4,2]
    row6 = [9,1,7]
    matrix2 = [row4,row5,row6]
    display_matrix(matrix1)
    print()
    display_matrix(matrix2)

def display_matrix(matrix1):
    index_row = 0
    while(index_row < len(matrix1)):
        index = 0
        print("|",end = "")
        while(index < len(matrix1[index_row])):
            print(" ",matrix1[index_row][index]," ",end  = "")
            index += 1
        print("|")  
        index_row +=1
     

setup()

LAB 6 Generic 15( Add two matrices )
def setup():
    row1 = [2,5,4]
    row2 = [9,5,6]
    row3 = [3,2,5]
    matrix1 = [row1,row2,row3]
    row4 = [5,6,4]
    row5 = [8,4,2]
    row6 = [9,1,7]
    matrix2 = [row4,row5,row6]
    plus_2_matrix(matrix1,matrix2)

def plus_2_matrix(matrix1,matrix2):
    index_row = 0
    while(index_row < len(matrix1)):
        index = 0
        print("|",end = "")
        while(index < len(matrix1[index_row])):
            print(" ",matrix1[index_row][index]+matrix2[index_row][index]," ",end  = "")
            index += 1
        print("|")  
        index_row +=1

LAB 6 Generic 16( Subtract two matrices )
def setup():
    row1 = [2,5,4]
    row2 = [9,5,6]
    row3 = [3,2,5]
    matrix1 = [row1,row2,row3]
    row4 = [5,6,4]
    row5 = [8,4,2]
    row6 = [9,1,7]
    matrix2 = [row4,row5,row6]
    plus_2_matrix(matrix1,matrix2)

def plus_2_matrix(matrix1,matrix2):
    index_row = 0
    while(index_row < len(matrix1)):
        index = 0
        print("|",end = "")
        while(index < len(matrix1[index_row])):
            print(" ",matrix1[index_row][index]-matrix2[index_row][index]," ",end  = "")
            index += 1
        print("|")  
        index_row +=1

LAB 6 Generic 17( Multiply two matrices )
def setup():
    row1 = [2,5,4]
    row2 = [9,5,6]
    row3 = [3,2,5]
    matrix1 = [row1,row2,row3]
    row4 = [5,6,4]
    row5 = [8,4,2]
    row6 = [9,1,7]
    matrix2 = [row4,row5,row6]
    plus_2_matrix(matrix1,matrix2)

def plus_2_matrix(matrix1,matrix2):
    index_row = 0
    index_result_row = 0
    const1 = 0
    while(index_result_row < len(matrix1)):
        index = 0
        index_result = 0
        const2 = 0
        print("| ",end = "")
        while(index_result < len(matrix2[index_row])):
            temp1 = matrix1[index_row+const1][index]*matrix2[index_row][index+const2]
            temp2 = matrix1[index_row+const1][index+1]*matrix2[index_row+1][index+const2]
            temp3 = matrix1[index_row+const1][index+2]*matrix2[index_row+2][index+const2]
            print(" ",temp1+temp2+temp3,end = " ")
            const2 += 1
            index_result += 1
        print(" |")  
        const1 += 1  
        index_result_row += 1      
         

setup()


LAB 6 Generic 18( Transpose a square matrix in place without creating a new matrix )
def setup():
    row1 = [2,5,4]
    row2 = [9,5,6]
    row3 = [3,2,5]
    matrix1 = [row1,row2,row3]
    row4 = [5,6,4,1]
    row5 = [8,4,2,1]
    row6 = [9,1,7,1]
    row7 = [0,0,0,0]
    matrix2 = [row4,row5,row6,row7]
    display_matrix(matrix2)
    transpose_squre_matric(matrix2)
    print()
    display_matrix(matrix2)

def transpose_squre_matric(matrix1):
    index_row = 0
    while(index_row < len(matrix1)):  
        index = index_row
        while(index < len(matrix1[index_row])):
            temp = matrix1[index_row][index]
            matrix1[index_row][index] = matrix1[index][index_row]
            matrix1[index][index_row] = temp
            index += 1
        index_row += 1  
     
def display_matrix(matrix1):
    index_row = 0
    while(index_row < len(matrix1)):
        index = 0
        print("|",end = "")
        while(index < len(matrix1[index_row])):
            print(" ",matrix1[index_row][index]," ",end  = "")
            index += 1
        print("|")
        index_row +=1      
         
setup()

LAB 6 Generic 19( Function that takes a string as an argument and prints out the string in large letters )
def setup():
    word = input("Draw : ")
    draw_alphabet(word)

def draw_alphabet(word):
    N_line1 = " #    # "
    N_line2 = " ##   # "
    N_line3 = " # #  # "
    N_line4 = " #  # # "
    N_line5 = " #   ## "
    alphabet_N = [N_line1,N_line2,N_line3,N_line4,N_line5]
    O_line1 = "  ####  "
    O_line2 = " #    # "
    O_line3 = " #    # "
    O_line4 = " #    # "
    O_line5 = "  ####  "
    alphabet_O = [O_line1,O_line2,O_line3,O_line4,O_line5]
    B_line1 = " #####  "
    B_line2 = " #    # "
    B_line3 = " #####  "
    B_line4 = " #    # "
    B_line5 = " #####  "
    alphabet_B = [B_line1,B_line2,B_line3,B_line4,B_line5]
    index_print = 0
    while(index_print < 5):
        index_word = 0
        while(index_word < len(word)):
            if(word[index_word] == "N"):
                print(alphabet_N[index_print],end = "")
            elif(word[index_word] == "O"):
                print(alphabet_O[index_print],end = "")
            elif(word[index_word] == "B"):
                print(alphabet_B[index_print],end = "")
            index_word += 1
        print()  
        index_print += 1  
                                     
setup()

ปัญหาที่พบแล้ววิธีแก้
 1) ไม่สามารถหลุดloop whileได้เพราะลืมใส่คำสั่งบวกค่า index ทุกครั้งที่ทำงานเสร็จ ( เพิ่มคำสั่ง index += 1 ต่อท้ายคำสั่งสุดท้ายใน loop while)
 2) โปรแกรมข้าม loop while ไปเพราะเงื่อนไขที่กำหนดผิด (แก้เงื่อนไขจาก index > len(value1) เป็น index < len(value1) แทน)
 3) โปรแกรมปริ้นค่าอะเรย์ 2 มิติบรรทัดแรกทุกตัว แต่บรรทัดถัดๆไปของอะเรย์ไม่มีการแสดงค่าออกมา เพราะกำหนดตัวแปร index ตัวที่สองผิดที่ (แก้คำสั่งจาก
  index_row = 0
  index_cols = 0
    while(index_row < len(matrix1)):
        while(index_cols < len(matrix1[index_row])):
. . .
เป็น
  index_row = 0
    while(index_row < len(matrix1)):
        index_cols = 0
        while(index_cols < len(matrix1[index_row])):
. . . )