Done
Def 함수이름(매개변수):
실행문장1
실행문장2
return 반환값(들)
다음 두 코드의 차이는? (글로벌 변수에 관하여)
a=0
def printVal(score, name):
a=0
print('score =', score, ' name = ', name)
a+=1
return a
while True:
score=int(input('score:'))
name=input('name:')
tt=printVal(score,name)
if tt==5:
break
print(tt, '회 입력')
a=0
def printVal(score, name):
global a
print('score =', score, ' name = ', name)
a+=1
return a
while True:
score=int(input('score:'))
name=input('name:')
tt=printVal(score,name)
if tt==5:
break
print(tt, '회 입력')
클래스 생성
class Sum1:
def__init__(self,n1,n2)
self.n1=n1
self.n2=n2
def add(self):
result=self.n1+self.n2
return result
a = Sum1(10,20)
print(a.add())
class gaesan:
def __init__(self,n1,n2):
self.n1=n1
self.n2=n2
def add(self):
return self.n1+self.n2
def sub(self):
return self.n1-self.n2
def mul(self):
return self.n1*self.n2
def div(self):
return self.n1/self.n2
상속
class gaesanMore(gaesan):
def mul2(self):
return self.n1**self.n2
오버라이딩
class gaesanRe(gaesan):
def div(self):
if self.n2 == 0:
print('error')
else:
return self.n1/self.n2
'''