제로베이스_데이터스쿨_230410스터디노트

김지태·2023년 4월 11일
0
post-thumbnail

05_047 모듈 / 모듈을 이용한 프로그래밍

def calculatorTotalPrice(gs):
if len(gs) <= 0:
print('구매 상품이 없습니다.')
return # 더 이상 프로그램이 실행할꺼리가 없어짐.

rate = 25
totalPrice = 0

rates = {1:5, 2:10, 3:15, 4:20}

if len(gs) in rates: # gs의 길이 값( len)이 rates 딕셔너리의 키 값으로 있다면 => 아마 벨류보다 키가 주 인듯

# gs 에 자료구조가 올텐데 자료구조의 길이가 있다면(1, 2, 3, 4 중 하나라면,), 기존의 할인율 25 (rate = 25) 을 바꿔줘야하니까
rate = rates[len(gs)]  # 1, 2, 3, 4  아니면 무조건 다 25%로 한다. / 그리고 이것도 딕셔너리 키 위주/ 그리고 딕셔너리에 [n] 이런식으로 가리킬 때 n은 순서가 아니라 키 값인듯 그래서 '딕셔너리이름[n]'이라고 하면 딕셔너리에 n 이란 키를 가진 value 값을 내보낸다

##### 이렇게 할인율을 결정했다. 

for g in gs: # 상품을 하나씩 가지고 온다
totalPrice += g (1- rate 0.01) # 상품 곱하기 할인율

return [rate, int(totalPrice)] # 0.01 곱하면 실수가 나오니까

def formatedNumber(n):
return format(n, ',')

discount 모듈 만들었으니 활용

import discount as dc

if name == 'main': # 전역변수 사용을 해본다?

flag = True
gs = []

while flag:
selectNumber = int(input('1. 구매, 2. 종료')) # 구매할건지 말건지 선택하게 한다.

if selectNumber == 1:
  goods_price = int(input('상품 가격 입력: '))

  gs.append(goods_price)  # 그럼 gs에는 물건 가격들이 쌓이겠네.
  
elif selectNumber == 2:
  result = dc.calculatorTotalPrice(gs)
  flag = False
  
  

print(f'할인율: {result[0]} %')
print(f'합계: {dc.formatedNumber(result[1])} 원')

출력
할인율: 10 %
합계: 49,770 원

클래스 개념 복습 다시(05_017 - 018 부분)

객체를 이용해서 프로그램을 만든다

객체를 어떻게 만드냐?

객체를 클래스로부터 생성이 된다.
클래스를 만들 때 속성과 기능을 정의하면 된다.
클래스를 우선 만든다음, 객체 생성을 하기 위해 생성자 호출을 한다.

클래스 만들기 -> 생성자 호출

호출을 하면 알아서 메모리에 객체가 생긴다. 이 때 특이한 점은 클래스는 딱 한 개만 만들었지만 객체는 마음대로 많이 만들 수 있다.
클래스 의 속성은 변수, 기능은 함수

생성자는 보이지 않고 숨어있다.

클래스 이름(a, b) -> 이렇게 입력한 것이 생성자를 호출한 것이다.
Car (a, b) 이렇게 호출하면 자동으로 init 메서드를 호출해준다.
전달해주므로서 속성 및 기능이 a, b 초기화되고 이를 생성자 호출이라 한다.
생성자 호출해서 객체가 초기화 되며(속성 값이 초기화) 객체가 생성됨

05_048 모듈 3 - 로또 머신

#####사용자가 총 5개의 숫자 입력 & 기계가 만들어낸 숫자 비교

로또 머신 난수로 숫자 6개, 보너스 숫자 1개 / 로또 머신은 난수 발생, 사용자가 입력한 번호 저장 기능도 필요
lotto module

import random

userNums = []; randNums = []; collNums = []

randBonusNum = 0

def setUserNums(ns): # 사용자가 번호 입력하면(매개변수) userNums에 넣도록 한다
global userNums
userNums = ns

def getUserNums(): # 사용자가 입력한 것을 빼온다?
return userNums

def setRandNums(): # 기계
global randNums
randNums = random.sample(range(1, 46), 6)

def getRandNums():
return randNums

def setBonusNum():
global randBonusNum

그러나, 보너스 숫자는 위에서 발생한 6개 숫자와 겹치면 안된다

while True: # 겹치지 않을 때까지 번호를 생성하는 기능이 필요하니까
randBonusNum = random.randint(1, 45)
if randBonusNum not in randNums:
break # 겹치지 않으면 그만 실행한다 / while 문 빠져나온다

def getBonusNum():
return randBonusNum

로또 결과 반환 함수 / 유저가 뽑은 숫자랑 기계가 뽑은 숫자 비교해서 다 collNums에 다 넣어야 한다.

def lottoResult():
global userNums
global randNums
global collNums

collNums = [] # 자료가 있으면 안되니 초기화를 미리 해준다
for un in userNums:
if un in randNums:
collNums.append(un)

if len(collNums) == 6:
print('1등 당첨')
print(f'번호 : {collNums}')

elif (len(collNums)) == 5 and (randBonusNum in userNums):
print('2등 당첨')
print(f'번호: {collNums}, 보너스 번호: {randBonusNum}')

elif len(collNums) == 5:
print(f'3등 당첨')
print(f'번호: {collNums}')

elif len(collNums) == 4:
print(f'4등 당첨')
print(f'번호: {collNums}')

elif len(collNums) == 3:
print(f'5등 당첨')
print(f'번호: {collNums}')

else:
print('꽝! 다음 기회에.')
print(f'기계번호: {randNums}')
print(f'Bonus 번호: {randBonusNum}')
print(f'user 번호: {userNums}')
print(f'correct 번호: {collNums}')

def startLotto():
n1 = int(input('번호 (1 ~ 45) 입력: '))
n2 = int(input('번호 (1 ~ 45) 입력: '))
n3 = int(input('번호 (1 ~ 45) 입력: '))
n4 = int(input('번호 (1 ~ 45) 입력: '))
n5 = int(input('번호 (1 ~ 45) 입력: '))
n6 = int(input('번호 (1 ~ 45) 입력: '))
selectNums = [n1, n2, n3, n4, n5, n6]

setUserNums(selectNums)
setRandNums()
setBonusNum()

lottoResult()

실행 파일

import lotto as lt

lt.startLotto()

####해답

import random

userNums = []; randNums = []; collNums = []
randBonuNum = 0

def setUserNums(ns):
global userNums
userNums = ns

def getUserNums():
return userNums

def setRandNums():
global randNums
randNums = random.sample(range(1, 46), 6)

def getRandNums():
return randNums

def setBonuNum():
global randBonuNum

while True:
    randBonuNum = random.randint(1, 45)
    if randBonuNum not in randNums:
        break

def getBonuNum():
return randBonuNum

def lottoResult():
global userNums
global randNums
global collNums

collNums = []
for un in userNums:
    if  un in randNums:
        collNums.append(un)

if len(collNums) == 6:
    print(f'1등 담첨!!')
    print(f'번호: {collNums}')

elif (len(collNums) == 5) and (randBonuNum in userNums):
    print(f'2등 담첨!!')
    print(f'번호: {collNums}, 보너스 번호: {randBonuNum}')

elif len(collNums) == 5:
    print(f'3등 담첨!!')
    print(f'번호: {collNums}')

elif len(collNums) == 4:
    print(f'4등 담첨!!')
    print(f'번호: {collNums}')

if len(collNums) == 3:
    print(f'5등 담첨!!')
    print(f'번호: {collNums}')

else:
    print('아쉽습니다. 다음 기회에~')
    print(f'기계 번호: {randNums}')
    print(f'보너스 번호: {randBonuNum}')
    print(f'선택 번호: {userNums}')
    print(f'일치 번호: {collNums}')

def startLotto():
n1 = int(input('번호(1~45) 입력: '))
n2 = int(input('번호(1~45) 입력: '))
n3 = int(input('번호(1~45) 입력: '))
n4 = int(input('번호(1~45) 입력: '))
n5 = int(input('번호(1~45) 입력: '))
n6 = int(input('번호(1~45) 입력: '))
selectNums = [n1, n2, n3, n4, n5, n6]

setUserNums(selectNums)
setRandNums()
setBonuNum()

lottoResult()

55강까지 들었으나 머리속에 하나도 남지 않는 것 같다. 내일은 진도를 나가지 말고 오늘 부분을 심도 있게 복습을 해야겠다.

진도를 못나갔다고 해서 불안해하지 말자.
진도 나가는 것이 중요한 것이 아니라, 내 머리 속에 넣는 것이 중요하다.

profile
데이터 분석가

0개의 댓글