1 2 3 4 5 | def para_func( * para): result = 0 for num in para: result = result + num return result |
변수선언
>>> hap = 0
실행
>>> hap = para_func(10,20,30)
>>> print(hap)
60
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | def para_func( * para): result = 0 for num in para: result = result + num return result #변수선언 hap = 0 v1,v2,v3 = 0 , 0 , 0 #메인코드 while True : v1 = int ( input ( "v1 = " )) v2 = int ( input ( "v2 = " )) v3 = int ( input ( "v3 = " )) hap = para_func(v1,v2,v3) print ( "hap=%d" % hap) if hap = = 0 : print ( "프로그램 종료" ) break |
v1 = 10
v2 = 20
v3 = 30
hap=60
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #모듈 -> 함수의 집합으로 다른 프로그램에서도 활용하고자 할 때 사용 def func1() : print ( "func1()함수 실행" ) def func2() : print ( "func2()함수 실행" ) def func3() : print ( "func3()함수 실행" ) def func4() : print ( "func4()함수 실행" ) def func5() : print ( "func5()함수 실행" ) func5() #실행할 함수 |
func5()함수 실행
1 2 3 4 5 6 7 8 9 | #콘솔(console) : 키보드 + 모니터 #쓰기용 : 변수명 - open("파일명","w") text = "통신응용프로그래밍실습" f = open ( "e:\swa\swa2.txt" , "w" ) f.write(text) #write 함수 f.close() print ( "파일저장 완료 : %s" % f) |
파일저장 완료 : <_io.TextIOWrapper name='e:\\swa\\swa2.txt' mode='w' encoding='cp949'>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | # Source_code 10-1 inFp = open ( "E:\swa\swa.txt" , "r" ,encoding = "utf-8" ) intStr = inFp.readline() print (intStr, end = "") intStr = inFp.readline() print (intStr, end = "") intStr = inFp.readline() print (intStr, end = "") inFp.close() |
손용호
밤새더니
학교와서
계속 잠만 자네 (세번째 줄까지만 출력)
1 2 3 4 5 6 7 8 9 10 11 12 13 | # Source_code 10-2 # while 문에 의한 처리 inFp = None inStr = "" inFp = open ( "E:\swa\swa.txt" , "r" ,encoding = "utf-8" ) while True : inStr = inFp.readline() if inStr = = "" : break print (inStr,end = "") inFp.close()<font face = "맑은 고딕, sans-serif" ><span style = "white-space: normal;" > < / span>< / font> |
손용호
밤새더니
학교와서
쿨쿨자네
으이그
1 2 3 4 5 6 7 8 9 10 | # Source_code 10-2 # list 문에 의한 처리 inFp = None inList, inStr = [], "" inFp = open ( "E:\swa\swa.txt" , "r" ,encoding = "utf-8" ) inList = inFp.readlines() for inStr in inList : print (inStr,end = "") inFp.close() |
손용호
밤새더니
학교와서
쿨쿨자네
으이그
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #source_code10-6 #파일검색 import os inFp = None fName, inList, inStr = " ",[]," " while True : fName = input ( "파일명을 입력하세요 : " ) if os.path.exists(fName) : inFp = open ( "E:\swa\swa.txt" , "r" ,encoding = "utf-8" ) inList = inFp.readlines() for inStr in inList : print (inStr,end = "") inFp.close() if fName = = "끝" : break if fName ! = "swa.txt" : print ( "%s 파일이 없습니다." % fName) print ( "프로그램을 종료합니다." ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #source_code10-6 #파일쓰기 outFp = None outStr = "" outFp = open ( "E:\swa\swa2.txt" , "a" ,encoding = "utf-8" ) while True : outStr = input ( "내용 입력 : " ) if outStr ! = "" : outFp.writelines(outStr + "\n" ) else : break outFp.close() print ( "정상적으로 파일에 썼습니다." ) |
'먼지 낀 책장사이 > Python' 카테고리의 다른 글
쓰레드 (0) | 2016.10.11 |
---|---|
상속, 오버라이딩, 툴킷 (0) | 2016.09.27 |
Raw파일 읽기, 클래스, 생성자 (0) | 2016.09.20 |
문자열, 구구단, 그림그리기 (0) | 2016.09.13 |
연산자, while 반복, for문, 리스트/사전/튜플 (0) | 2016.08.30 |