반응형
4강에서 작성한 코드를 객체지향 형태로 바꿔봅시다. 4강에서 작성한 코드는 아래와 같습니다.
mylist=[]
mnum=0
#def usem()
while True :
print() #한 줄 띄기
print('Adding member : press a')
print('searching member : press b')
print('using membership coupon : press c')
action=input('press button ')
if action=='a' :
print() #한 줄 띄기
#inputs
name = input('enter your name? ')
name = name.lower() # force lowercase
age = int(input('enter your age? '))
sex = input('enter your sex? ')
sex = sex.lower() # force lowercase
mcount=int(input('enter your membership count ')) #membership count
#define dict
dict={'mnum':mnum,'name':name,'age':age,'sex':sex,'mcount':mcount}
#add in list
mylist.append(dict)
#print info
print('Below is your information')
print(dict)
mnum=mnum+1
elif action=='b' :
while True :
print() #한 줄 띄기
mn = int(input('what is the membership number? ')) #input 값 숫자에 넣기
first_values=[]
for my_dict in mylist:
first_values.append(list(my_dict.values())[0])
if mn in first_values :
print(mylist[mn])
break
else :
print('none exist member')
continue
elif action=='c' :
while True :
print() #한 줄 띄기
num = int(input('what is the membership number? ')) #input 값 숫자에 넣기
first_values=[]
for my_dict in mylist:
first_values.append(list(my_dict.values())[0])
if num in first_values :
if mylist[num]['mcount']<1 :
print('We cannot deduct it because you do not have a membership coupon.')
break
else :
mylist[num]['mcount']=mylist[num]['mcount']-1
print()
print('below is your updated info')
print(mylist[num])
break
else :
print('none exist member')
continue
else :
print()
print('no button exist')
아래는 클래스를 사용하여 객체지향 형태로 바꾼 것입니다.
class member():
def __init__(self,name,age,sex,mcount,mnum) :
self.name=name
self.age=age
self.sex=sex
self.mcount=mcount
self.mnum=mnum
def show(self):
print(' Name:', self.name)
print(' age:', self.age)
print(' sex:', self.sex)
print(' mcount:', self.mcount)
print()
mylist=[]
mnum=0
#def usem()
while True :
print() #한 줄 띄기
print('Add member : press a')
print('search member : press b')
print('using membership coupon : press c')
action=input('press button ')
if action=='a' :
print() #한 줄 띄기
#inputs
name = input('enter your name? ')
name = name.lower() # force lowercase
age = int(input('enter your age? '))
sex = input('enter your sex? ')
sex = sex.lower() # force lowercase
mcount=int(input('enter your membership count ')) #membership count
#define dict
mylist.append(member(name,age,sex,mcount,mnum))
#print info
print('Below is your information')
print('')
mnum=mnum+1
elif action=='b' :
while True :
print() #한 줄 띄기
mn = int(input('what is the membership number? ')) #input 값 숫자에 넣기
first_values=[]
for memb in mylist:
first_values.append(memb.mnum)
if mn in first_values :
mylist[mn].show()
break
else :
print('none exist member')
continue
elif action=='c' :
while True :
print() #한 줄 띄기
num = int(input('what is the membership number? ')) #input 값 숫자에 넣기
first_values=[]
for memb in mylist:
first_values.append(memb.mnum)
if num in first_values :
if mylist[num].mcount<1 :
print('We cannot deduct it because you do not have a membership coupon.')
break
else :
mylist[num].mcount=mylist[num].mcount-1
print()
print('below is your updated info')
mylist[num].show()
break
else :
print('none exist member')
continue
else :
print()
print('no button exist')
반응형
'10. 클래스(class) > 객체지향 이해하기' 카테고리의 다른 글
[파이썬 클래스와 객체지향] 6. 객체지향 2단계 (0) | 2023.05.24 |
---|---|
[파이썬 클래스와 객체지향] 5. 객체지향 1단계 (0) | 2023.05.24 |
[파이썬 클래스와 객체지향] 4. 절차지향 3단계 (0) | 2023.05.23 |
[파이썬 클래스와 객체지향] 3. 절차지향 2단계 (0) | 2023.05.21 |
[파이썬 클래스와 객체지향] 2. 절차지향 1단계 (0) | 2023.05.16 |
댓글