แสดงบทความที่มีป้ายกำกับ Lab 5 แสดงบทความทั้งหมด
แสดงบทความที่มีป้ายกำกับ Lab 5 แสดงบทความทั้งหมด

วันอังคารที่ 29 กันยายน พ.ศ. 2558

Syntax Error for Lab 5.

1.ใส่ชื่อตัวแปรไม่ตรงกัน เช่น

def setup():
   string = 'thailand'
   lasttring = 'and' <<<<<<<<<<<<< ผิดตรงนี้ไงครับ laststring สิ ไม่ใช่ lasttring อิอิ
   assert my_endswith(string, laststring) == True

setup()

วิธีแก้คือ แก้ไขชื่อให้ตรงกัน โปรแกรมเราก็จะสามารถรันได้ตามปกติ

2.ลืมส่งค่ากลับ เช่น

def my_startswith(string, firststring):
   result = False
   i = 0
   if (len(string) > len(firststring)):
      while (i < len(firststring)):
         if (string[i] == firststring[i]):
            result = True
         else:
            result = False
         i = i+1

โปรแกรมก็ไม่สามารถรู้ผลสิ วิธีแก้คือ ใส่ return result ไปซะ ดังนี้

def my_startswith(string, firststring):
   result = False
   i = 0
   if (len(string) > len(firststring)):
      while (i < len(firststring)):
         if (string[i] == firststring[i]):
            result = True
         else:
            result = False
         i = i+1
   return result <<<<<<<<<<<<<<<<<<<<<< นี้ไง ใส่มาแล้ว แค่นี้เราก็จะสามารถรันโปรแกรมได้ตามปกติ

3.ลืมอัพเดทค่า

def my_strip(string, strip):
   wordwithoutstrip = ''
   i = 0
   while (i < len(string)):
      if (i < len(strip)):
         wordwithoutstrip = wordwithoutstrip + string[i]

   return wordwithoutstrip แบบนี้เราก็จะรันโปรแกรมไม่ได้นะครับ วิธีแก้คือ

def my_strip(string, strip):
   wordwithoutstrip = ''
   i = 0
   while (i < len(string)):
      if (i < len(strip)):
         wordwithoutstrip = wordwithoutstrip + string[i]
      i = i+1 <<<<<<<<<<<<<<<<<<< ก็อัพเดทค่ามันซะสิ เราก็จะสามารถรันโปรแกรมได้ตามปกติ
   return wordwithoutstrip

my_endswith.

def my_endswith(string, laststring):
   result = False
   i = 0
   if (len(string) > len(laststring)):
      while (i < len(laststring)):
         if (string[i+(len(string)-len(laststring))] == laststring[i]):
            result = True
         else:
            result = False
         i = i+1
   return result

def setup():
   string = 'thailand'
   laststring = 'and'
   assert my_endswith(string, laststring) == True

setup()

my_startswith.

def my_startswith(string, firststring):
   result = False
   i = 0
   if (len(string) > len(firststring)):
      while (i < len(firststring)):
         if (string[i] == firststring[i]):
            result = True
         else:
            result = False
         i = i+1
   return result

def setup():
   string = 'thailand'
   firststring = 'sun'
   assert my_startswith(string, firststring) == False

setup()

my_strip.

def my_strip(string, strip):
   wordwithoutstrip = ''
   i = 0
   while (i < len(string)):
      if (i < len(strip)):
         wordwithoutstrip = wordwithoutstrip + string[i]
      i = i+1
   return wordwithoutstrip

def setup():
   string = 'thailand    '
   strip = '        '
   print("world is", my_strip(string, strip))
   assert my_strip(string, strip) == 'thailand'

setup()

my_replace.

def my_replace(string, replace):
   new = ''
   i = 0
   while (i < len(string)):
      if (i < len(replace)):
         new = new + replace[i]
      elif (i >= len(replace)):
         new = new + string[i]
      i = i+1
   return new

def setup():
   string = 'thailand'
   replace = 'blue'
   print("Old world is", string, "and New world is", my_replace(string, replace))
   assert my_replace(string, replace) == 'blueland'

setup()

วันจันทร์ที่ 28 กันยายน พ.ศ. 2558

my_find.

def setup():
      my_find = "thailand"
      i = 0
      indexi = i
      while (i < len(my_find)):
         if ("i" == my_find[i]):
            indexi = i
         i += 1
      print(indexi)
      assert (indexi == 3)
setup()

my_count.

def setup():
      my_count = "thailand"
      i = 0
      count = 0
      check = True
      while (i < len(my_count)):
         if ("a" == my_count[i]):
            check = True
            count += 1
         else:
            check = False
         i += 1
      print(count)
      assert (count == 2)
setup()
     

convert a number from base 10 to base 2.

def setup():
      baseten = 3
      basetwo = ""
      while (baseten > 0):
         if (baseten % 2 == 0):
            basetwo = "0" + basetwo
            baseten = baseten/2
         elif (baseten % 2 == 1):
            basetwo = "1" + basetwo
            baseten = (baseten/2) - 0.5
      print(basetwo)

setup()
     

วันจันทร์ที่ 21 กันยายน พ.ศ. 2558

Increase/decrease values in array.

def new(array):
   i = 0
   sum = 0
   new = 0
   while(i < len(array)):
      new = array[i] + 2
      sum += new
      i += 1
      print("new value index ", i, "is ", new)
   print("sum = ", sum)
 
def setup():
   array = [3, 8, 44, -6, -2, 44, -5]
   new(array)
 
setup()

Find average of values in array.

def average(array):
   i = 0
   sum = 0
   n = 0
   average =0
   while(i < len(array)):
      sum += array[i]
      i += 1
      n += 1
   average = sum/n
   print("average of values in this array is ", average)

def setup():
   array = [3, 8, 44, -6, -2, 44, -5]
   average(array)

setup()

Find/count number of positive values in array.

def countpositive(array):
   i = 0
   positive = 0
   while(i < len(array)):
      if(array[i] > 0):
         positive += 1
      i += 1
   print("number of positive have ", positive, " number")
   
def setup():
   array = [3, 8, 44, -6, -2, 44, -5]
   countpositive(array)
 
setup()

Find sum of positive values in array.

def sumpositive(array):
   i = 0
   sum = 0
   while(i < len(array)):
      if(array[i] > 0):
         sum = sum + array[i]
      i += 1
   return sum

def setup():
   array = [3, 8, 44, -6, -2, 44, -5]
   print("sum positive = ",sumpositive(array))
 
setup()

Find sum of values in array.

def sumnumber(a):
   i = 0
   sum = 0
   while(i < len(a)):
      sum = sum + a[i]
      i += 1
   return sum
   
def setup():
   array = [3, 8, 44, -6, -2, 44, -5]
   sumnumber(array)
   print("sum number = ", sumnumber(array))
 
setup()

Find index of (the last) maximum value in array.

def maximumfirst(array):
   i = 6
   maximum = array[i]
   indexmax = i
   while(i >= 0):
      if(maximum < array[i]):
         maximum = array[i]
         indexmax = i
      i -= 1
   print("Maximum = ", maximum)
   print("index of maximum (the last) = ", indexmax)    
   
def setup():
   array = [3, 8, 44, -6, -2, 44, -5]
   maximumfirst(array)
 
setup()

Find index of (the first) maximum value in array.

def maximumfirst(array):
   i = 0
   maximum = array[i]
   indexmax = i
   while(i < len(array)):
      if(maximum < array[i]):
         maximum = array[i]
         indexmax = i
      i += 1
   print("Maximum = ", maximum)
   print("index of maximum (the first) = ", indexmax)    
   
def setup():
   array = [3, 8, 44, -6, -2, 44, -5]
   maximumfirst(array)
 
setup()

Find the maximum value in array.

def maximum(array):
   i = 0
   maximum = array[i]
 
   while(i < len(array)):
      if(maximum < array[i]):
         maximum = array[i]
       
      i += 1
   print("Maximum = ", maximum)
     
   
def setup():
   array = [3, 8, 44, -6, -2, 44, -5]
   maximum(array)
 
setup()

Display elements (value) of array and its index.

def printArray(array):
   i = 0
   while( i < len(array)):
      print("index", i, "value", array[i])
      i += 1  
   
def setup():
   array = [3, 8, 44, -6, -2, 44, -5]
   printArray(array)
 
setup()

Link Video Presentation Resort Managemant System Project.

Video Presentation Resort Managemant System Project. จัดทำโดย พากษ์เสียง: คุณาสิน  ทองมณี  5801012620011 ลำดับภาพ: สุพิชชา  ศรีศิริ...