วันจันทร์ที่ 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])):
. . . )
สมัครสมาชิก:
ส่งความคิดเห็น (Atom)
ไม่มีความคิดเห็น:
แสดงความคิดเห็น