제로베이스 데이터 취업 스쿨 - 12일차(6/13)

수야·2023년 6월 13일
0

파이썬 중급 문제풀이

[연습문제] 함수(01)

def CalMethod() :
        print('-'*100)
        method = int(input('1.덧, 2.뺄 3.곱, 4.나눗셈 5.나머지, 6.목, 7.제곱승, 8. 종료'))
        if method != 8 :
            num1 = float(input('첫번째 숫자 입력'))
            num2 = float(input('두번째 숫자 입력'))
            if method == 1 :
                resut = f'{num1} + {num2} = {num1+num2}\n'
                tool = '-'*100
                return resut+tool
        else :
            resut ='종료. 수고하셨습니다.\n'
            tool = '-' * 100
            return resut+tool

print(CalMethod())

처음에는 하고나면 계속None이 나오는거임..
함수에서 None을 안나오게하는법은
return a
일때
a = 'b'여야지
a=print('b')하면 non이 나옴
https://www.codeit.kr/community/questions/UXVlc3Rpb246NWUzNDUyMjU4MGU1MTMzNzNkOTYxNDNi
-> 함수만 호출되게 하면 None이 출력되지 않습니다.

[연습문제] 함수(02)

def moveAbout() :
    print('-'*100)
    speed = float(input('속도(km/h)입력'))
    hour = float(input('시간(h)입력'))
    minute = float(input('시간(m)입력'))
    move = speed*(hour+(minute/60))
    print(f'{speed}속도로 {hour}시간 {minute}분동안 이동한 거리 : {move}')
    print('-' * 100)
moveAbout()

분을 시간으로 바꾸려면 /60

[연습문제] 함수(03)

childPrice = 18000 #24개월 미만
infantPrice = 25000 #만 12세 미만
adultPrice = 25000 #만 12세 이후
discountPrice = 0.5 #국가유공자, 장애우

def tick() :
    countChild = int(input('유아 입력'))
    countDiscountChild = int(input('할인 대상 유아 입력'))
    countInfant = int(input('소아 입력'))
    countDiscountInfant = int(input('할인 대상 소아 입력'))
    countAdult = int(input('성인 입력'))
    countDiscountAdult = int(input('할인 대상 성인 입력'))

    resultcountChild = countChild * childPrice
    resultcountDiscountChild = countDiscountChild * childPrice * discountPrice
    resultcountInfant = infantPrice * countInfant
    resultcountDiscountInfant = infantPrice * countInfant * discountPrice
    resultcountAdult = adultPrice * countAdult
    resultcountDiscountAdult = adultPrice * countAdult * discountPrice

    total = countChild + countDiscountChild + countInfant + countDiscountInfant+ countAdult + countDiscountAdult
    totalPrice = resultcountChild + resultcountDiscountChild  + resultcountInfant+ resultcountDiscountInfant + resultcountAdult+ resultcountDiscountAdult
    print('='*50)
    print(f'유아 {countChild}명 요금 : {resultcountChild}\n'
          f'유아 할인대상 {countDiscountChild}명 요금 : {resultcountDiscountChild}\n'
          f'소아 {countInfant}명 요금 : {resultcountInfant}\n'
          f'소아 할인대상 {countDiscountInfant}명 요금 : {resultcountDiscountInfant}\n'
          f'성인 {countAdult}명 요금 : {resultcountAdult}\n'
          f'성인 할인대상 {countDiscountAdult}명 요금 : {resultcountDiscountAdult}\n')
    print('='*50)
    print(f'Total : {total}명\n'
          f'TotalPrice : {totalPrice}원')
    print('=' * 50)

tick()

.0나오는거 int처리
숫자 , 찍히는거 처리 필요.format(format(a, ','))

[연습문제] 함수(04)

import math


def fac() :
    num = int(input('input number :'))
    result = math.factorial(num)
    print('{}'.format(format(result, ',')))

fac()

팩토리얼 함수 쓸라면 math를 호출해야 하는것이고,
콤마찍을라면 포맷안에 포맷하고 찍어야함

base = int(input('예치금 (원)'))
year = int(input('기간(년)'))
percent = int(input('연 이율 (%)'))
usePercent = percent*0.01
print(usePercent)
def bankCal(base, year, usePercent) :
    totalmoney = 0
    totalratemoney = 0
    for i in range(year+1) :
        totalratemoney += base * usePercent
    totalmoney = totalratemoney +base
    formatedResult = format(totalmoney, ',')
    print('[단리 계산기]')
    print('='*30)
    print('예치금\t:{}원'.format(format(base, ',')))
    print(f'예치기간\t:{year}년')
    print(f'연 이율\t:{percent}%')
    print('-'*30)
    print(f'{year}년 후 총 수령액 : {formatedResult}원')
    print('=' * 30)

bankCal(base, year, usePercent)

와 나는 은행 관련된건 아예 못한다...
복리 계산이 뭐가 잘못된건데 절대 뭔지 모르겠으 ㅁ

[연습문제] 함수(05)

a1 = int(input('a1입력'))
step = int(input('공차입력'))
n = int(input('n입력'))

def stepClass(a1, step, n) :
    for i in range(1,n+1):
        num = a1 + step *(i-1)
        sum =  int ((a1 + num) * i / 2)
        print(f'{i}번쨰 항의 값 : {num}\n'
              f'{i}번쨰 항까지의 값 : {sum}')

stepClass(a1, step, n)

등차수열에서
N번째값 = 초항 + 공차 *(N-1)

등차수열에서 n번째 까지합은
(초항 + N번째값) * N / 2

[연습문제] 함수(06)

a1 = int(input('a1입력'))
step = int(input('공비입력'))
n = int(input('n입력'))

def stepClass(a1, step, n) :
    for i in range(1,n+1):
        num = a1*(step**i)
        sum = int(a1*(1-(step**i))/(1-step))
        print(f'{i}번쨰 항의 값 : {num}\n'
              f'{i}번쨰 항까지의 값 : {sum}')

stepClass(a1, step, n)

등비수열 n번쨰 값 확인법
n번째값 = 초항(공비^n-1)
n번쨰값까지의 합 = 초항
(1-공비^n)/1-공비

[연습문제] 모듈(01)

def Score(sub1,sub2,sub3,sub4,sub5) :
    total = sub1+sub2+sub3+sub4+sub5
    print(f'총점 : {total}')
    average = total/5
    print(f'평균 : {average}')

    passAver = 60
    nonpassSub=40
    scoreList = [sub1,sub2,sub3,sub4,sub5]
    for i in range(1,6):
        if scoreList[i-1] >=nonpassSub :
            print(f'{scoreList[i-1]}: Pass')
        else:
            print(f'{scoreList[i-1]}: Fail!')
    if average >passAver:
        print('Final Pass')
    else:
        print('Final Fail!')

if scorelist [i]라 했지만, 위에서 i를 0부터 시작을 안했으니 마지막 i가 들어가면 개수가 부족하지.
그래서 실제 리스트에서 i를 찾을 때는 -1.

[연습문제] 모듈(02)

>모듈
def discountRate(count) :
    rate = 25
    if count == 1:
        rate = 5
    elif count == 2:
        rate = 10
    elif count == 3:
        rate = 15
    elif count == 4:
        rate = 20
    return rate

def discountPrice(count, totalPrice) :
    rate = 25
    if count == 1:
        rate = 5
    elif count == 2:
        rate = 10
    elif count == 3:
        rate = 15
    elif count == 4:
        rate = 20

    result =int(totalPrice*((100-rate)/100))
    formatResult = format(result, ',')
    return formatResult
>실행파일
import example as ex
if __name__ == '__main__':
    flag = True
    totalPrice = 0
    count = 0

    while flag:
        selectNum = int(input('1.구매 2.종료'))
        if selectNum == 1 :
            price = int(input('상품 가격 입력'))
            totalPrice +=price
            count += 1

        elif selectNum == 2 :
            flag = False

print(f'할인율 : {ex.discountRate(count)}')
print(f'합계 : {ex.discountPrice(count, totalPrice)}원')

if name == 'main'
이거를 생각못했스 ㅠㅠ

[연습문제] 모듈(03)

>모듈
import random
lN = random.sample(range(1,46),6)
bonous = random.sample(range(1,46),1)
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)입력'))

userNumList = [n1, n2,n3,n4,n5,n6]
sameNum = []
def lottoNum() :
    return lN
def bonousNum() :
    return bonous

def userNum():
    global userNumList
    return userNumList

def priceNum():
    global userNumList
    global lN
    global bonous
    global sameNum
    for un in userNumList:
        if un in lN :
            sameNum.append(un)
        if un ==bonous:
            sameNum.append(un)
    if len(sameNum) == 6 :
        result = '1등당첨'
    elif len(sameNum) == 5 :
        result = '2등당첨'
    elif len(sameNum) == 4 :
        result = '3등당첨'
    else:
        result = '다음 기회에..'
    totalResult = f'{sameNum}\n{result}'
    return totalResult
>실행파일
import example as ex

print(f'기계번호 : {ex.lottoNum()}')
print(f'보너스번호 : {ex.bonousNum()}')
print(f'선택번호 : {ex.userNum()}')
print(f'일치번호 : {ex.priceNum()}')

얼레벌레... 돌아가는..... 코딩...

profile
수야는 코린이에서 더 나아갈거야

0개의 댓글