วันอาทิตย์ที่ 4 ตุลาคม พ.ศ. 2558

Lab5-My count

def my_count(string,alphabet):
   i = 0
   numAlpha = 0
   while(i<len(string)):
      if(string[i] == alphabet):
         numAlpha+=1
      i+=1
   return numAlpha

def setup():
   S = 'Straightforward'
   alp = 'r'
   assert(my_count(S,alp) == 3 )
   print('number of',alp,'in',S,'is',my_count(S,alp))
setup()

Result
number of r in Straightforward is 3

ไม่มีความคิดเห็น:

แสดงความคิดเห็น