วันพฤหัสบดีที่ 18 สิงหาคม พ.ศ. 2559

Building Software : raw_input รับค่า Arg ทาง command line

โค๊ต : use raw_input

def setup():
  multiplier = int(raw_input(' multiplier is : '))
  count = 1
  while(count <= 12):
    result = multiplier * count
    print multiplier ,' x ', count ,' = ', result
    count = count +1



setup()


ผลลัพธ์

multiplier is :

จากนั้นเติมเลขลงไป ได้

 multiplier is : 7
7  x  1  =  7
7  x  2  =  14
7  x  3  =  21
7  x  4  =  28
7  x  5  =  35
7  x  6  =  42
7  x  7  =  49
7  x  8  =  56
7  x  9  =  63
7  x  10  =  70
7  x  11  =  77
7  x  12  =  84






...............................................................................................................................
โค๊ตที่ 2 : use arg in command line

import sys

def setup():
  jump_distant = int(sys.argv[1])
  total_distant = int(sys.argv[2])
  round_jump = total_distant /jump_distant # calculation
  if((total_distant % jump_distant) != 0): # check
    round_jump = round_jump + 1
  print 'frog must jump' ,round_jump, 'round'



setup()

ผลลัพธ์ :
 xxxxxxxxxxxxxxxx:~/ดาวน์โหลด/Work$ python frogAr.py 3 57
frog must jump 19 round










..............................................................................................................................
โค๊ตที่ 3 :  use open file





def setup():
  file_input = open('file','r') # open file
  jump_distant = int(file_input.readline()) # read next line
  total_distant = int(file_input.readline())
  file_input.close # close file
  round_jump = total_distant /jump_distant # calculation
  if((total_distant % jump_distant) != 0): # check
    round_jump = round_jump + 1
  print 'frog must jump' ,round_jump, 'round'



setup()

ผลลัพธ์ :

 xxxxxxxxxxxxxxxxxxxxx:~/ดาวน์โหลด/Work$ python frog.py file
frog must jump 6 round




# file file have number 2 line first is 5 second is 27



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

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