'''
#nonlocal 변수사용하
def a():
x=10
y=100
def b():
x=20
def c():
nonlocal x
nonlocal y
x+=30
y+=300
print(x)
print(y)
c()
b()
a()
#global 전역 변수사용하기
x=1
def a():
x=10
y=100
def b():
x=20
def c():
global x
x+=30
print(x)
c()
b()
a()
#클로저 사용하기 사용자가건들이지않는 매개변수들이 존재하고 이를 감추고 함수를 사용할때 쓴다
#클로저는 함수를 둘러싼 환경을 유지했다가 나중에 다시 사용하는 함수임
def calc():
a=3
b=5
def mul_add(x):
return a*x+b
return mul_add
c=calc()
print(c(1),c(2),c(3),c(4),c(5))
def calc():
a=3
b=5
return lambda x:a*x+b
c=calc()
print(c(1),c(2),c(3),c(4),c(5))
#클로저의 지역변수 사용하기
def calc():
a=3
b=5
total=0 #mul_add외부에 있기 때문에 값이 변한상태로 유지됨
def mul_add(x):
nonlocal total
total = total+a*x+b
print(total)
return mul_add
c=calc()
print(c(1),c(2),c(3))
def calc():
a=3
b=5
def mul_add(x):
total=0
total = total+a*x+b
print(total)
return mul_add
c=calc()
print(c(1),c(2),c(3))
#연습문제 호출횟수를세는 함수
def counter():
i = 0
def count():
nonlocal i
i+=1
return i
return count
c= counter()
for i in range(10):
print(c(),end=' ')
#파일 열고 3줄까지 읽어오기
infp = open("c:/data1.txt","r",encoding="utf-8")
instr = infp.readline()
print(instr,end=' ')
instr = infp.readline()
print(instr,end=' ')
instr = infp.readline()
print(instr,end=' ')
instr = infp.readline()
print(instr,end=' ')
infp.close()
#반복문으로 문서의 끝까지 읽어오기
infp= None
instr=""
infp=open("c:/data1.txt","r",encoding="utf-8")
while True:
instr = infp.readline()
if instr == "": #더이상 읽어올 텍스트가 없음(EOF)
break
print(instr,end="")
infp.close()
#readlines() 파일 내용을 통으로 리스트에 저장
infp=open("c:/data1.txt","r",encoding="utf-8")
inlist=[]
inlist=infp.readlines()
print(inlist)
infp.close()
infp=None
inlist=[]
instr=""
infp=open("c:/data1.txt","r",encoding="utf-8")
inlist=infp.readlines()
for i in inlist:
print(i,end='')
infp.close()
#파일명을 입력받아서 출력하기
filename=input('파일명을 입력하세요 : ')
infp= None
instr=""
infp=open(filename,"r",encoding="utf-8")
while True:
instr = infp.readline()
if instr == "": #더이상 읽어올 텍스트가 없음(EOF)
break
print(instr,end="")
infp.close()
#새로운 파일을 만들어서 입력해주기
outfp=None
outstr=""
outfp=open("data2.txt", "w", encoding='utf-8')
while True:
outstr=input('내용입력 : ')
if outstr != "":
outfp.write(outstr+"\n")
else:
break
outfp.close()
print('작동')
#복사본 만들기
infp,outfp=None,None
instr=""
infp=open("data1.txt","r",encoding="utf-8")
outfp=open("data1_copy.txt", "w", encoding='utf-8')
while True:
outstr = infp.readline()
if outstr != "": #더이상 읽어올 텍스트가 없음(EOF)
outfp.write(outstr)
else:
break
infp.close()
outfp.close()
print('복사 완료')
#for문으로 카피하기
infp,outfp=None,None
instr=""
infp=open("data1.txt","r",encoding="utf-8")
outfp=open("data1_copy2.txt", "w", encoding='utf-8')
inlist= infp.readline()
for instr in inlist:
outfp.write(instr)
infp.close()
outfp.close()
print('복사 완료')
infp,outfp=None,None
instr=""
infp=open("data1.txt","r",encoding="utf-8")
outfp=open("data1_copy2.txt", "w", encoding='utf-8')
inlist= infp.readline()
outfp.writelines(inlist)
infp.close()
outfp.close()
print('복사 완료')
#암호화와 해독
#변수선언 ord()-문자의 고유한 숫자를 알려줌 chr()숫자에 해당하는문자를 알려줌
infp,outfp=None,None
instr,outstr="",""
i=0
secu=0
#메인코드부분
secuyn=input('1.암호화 2.암호해석중 선택 : ')
infname=input('입력 파일명을 입력하세요 : ')
outfname=input('출력 파일명을 입력하세요 : ')
if secuyn =="1":
secu=100
elif secuyn=="2":
secu=-100
infp=open(infname,"r",encoding="utf-8")#파일을 읽기
outfp=open(outfname, "w", encoding='utf-8')#파일을 쓰기
while True:
instr = infp.readline() #한줄씩읽어옴
if not instr: #모든 문자열을 읽어온후 아무것도 없으면 멈춤
break
outstr=""#내보낼 문자를 만들어줌
for i in range(0,len(instr)):
ch=instr[i] #한글자씩 읽어와서
chnum=ord(ch) #문자의고유한 숫자를 알려줌
chnum+=secu #고유 숫자에 암호화할 넘버를 넣어줌
ch2=chr(chnum) #번호에 해당하는 문자를 찾아줌
outstr+=ch2 #변화가 완료된 문자를 내보낼 문자에 더해줌
outfp.write(outstr)#암호화,해석완료된 내용을출력파일에 써줌
outfp.close()
infp.close()
print('%s-->%s 변환 완료'%(infname,outfname))
#시스템 상에서 파이썬 파일을 불러와서 메모장만들기
import sys
option = sys.argv[1]
if option =='-a' :
memo = sys.argv[2]
f= open('memo.txt','a')
f.write(memo)
f.write('\n')
f.close
elif option == '-v':
f = open('memo.txt')
memo=f.read()
f.close()
print(memo)
#내가 풀이한 방식 식패
infp= None
instr=""
inlist=[]
infp=open("Eng_LowData.txt","r",encoding="utf-8")
for i in range(199):
instr = infp.readline() #문자열을 한줄씩 읽어옴
if instr != "\n": #\n이 아닐경우에
inlist.append(instr) #리스트에 추가해줌
infp.close()
import random
import time
input('enter 입력시 시작')
timestart=time.time() #시간측정 시작
for i in range(5): #5문제 제출
rannum=random.randint(0,len(inlist))
q = inlist[rannum] #랜덤으로 문제 출력
print(q)
ans=input('입력 :') #사용자로부터 답을 입력받음
if q == ans:
print('정답')
elif q !=ans:
print('오답')
continue
timeend=time.time()
howlong = timeend-timestart
print(howlong)
#타자 연습 만들기 정답
import random
import time
infp= None
instr=""
w=[]
infp=open("Eng_LowData.txt","r",encoding="utf-8")
while True:
instr = infp.readline()
if instr == "":
break
elif instr !='\n':
print(instr,end='')
w.append(instr.rstrip())
infp.close()
#위에 코드까지 w라는 list에 띄워쓰기 제외 실제 단어들만드올 리스트 만
n=1
print('enter 입력시 시작')
input()
timestart=time.time() #시간측정 시작
q=rnadom.chice(w)
while n<=5:
print('*문제',n)
print(q)
x= input()
if q ==x :
print('통과')
n+=1
q = random.choice(w)
else:
print('초타')
endtime= time.time()
et = endtime -timestart
et= format(et,'.2f')
print('타자시간 : ',et,'초')
# unit GUI Tkinter
import tkinter as tk
root = tk.Tk()
lbl = tk.Label(root,text="Educoding",underline =3)
lbl.pack
txt = tk.Entry(root)
txt.pack()
btn = tk.Button(root, text = "ok",activebackground='red',width=5)
btn.pack()
root.mainloop()
import tkinter as tk #Tkinter 라이브러리 임포트
root = tk.Tk() #tk 객체인스턴스 생성(비어있는 윈도우 창)
#라벨생성
label = tk.Label(root,text="hello world")
#라벨배치
label.pack()
#root 표시
root.mainloop()
import tkinter as tk #Tkinter 라이브러리 임포트
root = tk.Tk() #tk 객체인스턴스 생성(비어있는 윈도우 창)
def func():
print('pushed')
button = tk.Button(root, text = "push!", command= func)
button.pack()
root.mainloop()
import tkinter as tk
root = tk.Tk()
def func():
label.config(text='pushed') #config는 속성을 바꿔
label=tk.Label(root,text='push button')
label.pack()
button=tk.Button(root, text='push!',command=func)
button.pack()
root.mainloop()
'''
'First step > AI 기초반' 카테고리의 다른 글
[TIL]21.06.24 PyQt ,class기초 (0) | 2021.06.24 |
---|---|
[TIL]21.06.23 GUI기초2,계산기만들기 (0) | 2021.06.23 |
[TIL]21.06.21 (0) | 2021.06.23 |
[TIL]21.06.18 (1) | 2021.06.23 |
[TIL]21.06.17 (0) | 2021.06.23 |