[4일차] 파이썬 기초문풀 2~3(연산자)

하은·2023년 10월 18일
0

연산자(산술연산자, 비교연산자, 논리연산자)

- 다양한 연산자를 이용한 프로그래밍

1. 상품가격과 지불금액을 입력하면 거스름돈을 계산하는 프로그램을 만들어보자. 단, 거스름돈은 지폐와 동선의 개수를 최소로 하고, 1원단위는 절사한다.

money50000 = 50000 ; money10000 = 10000 ; money5000 = 5000
money1000 = 1000 ; money500 = 500; money100 = 100 ; money10 = 10

money50000cnt = 0 ; money10000cnt = 0 ; money5000cnt = 0
money1000cnt = 0 ; money500cnt = 0 ; money100cnt = 0 ; money10cnt = 0

productPrice = int(input('상품 가격 입력: '))
payPrice = int(input('지불 금액: '))

if payPrice > productPrice:
    changeMoney = payPrice - productPrice
    changeMoney = (changeMoney // 10) * 10
    print('거스름돈: {}(원단위 절사)'.format(changeMoney))

if changeMoney > money50000:
   money50000cnt = changeMoney // money50000 #5만원 잔액
   changeMoney %= money50000 # 5만원으로 나눈 나머지

if changeMoney > money10000:
    money10000cnt = changeMoney // money10000
    changeMoney %= money10000

if changeMoney > money5000:
    money5000cnt = changeMoney // money5000
    changeMoney %= money5000

if changeMoney > money1000:
    money1000cnt = changeMoney // money1000
    changeMoney %= money1000

if changeMoney > money500:
    money500cnt = changeMoney // money500
    changeMoney %= money500

if changeMoney > money100:
    money100cnt = changeMoney // money100
    changeMoney %= money100

if changeMoney > money10:
    money10cnt = changeMoney // money10
    changeMoney %= money10

print('-' * 30)
print('50,000 {}장'.format(money50000cnt))
print('10,000 {}장'.format(money10000cnt))
print('5,000 {}장'.format(money5000cnt))
print('1,000 {}장'.format(money1000cnt))
print('500 {}개'.format(money500cnt))
print('100 {}개'.format(money100cnt))
print('10 {}개'.format(money10cnt))
print('-' * 30)

-->
상품 가격 입력: 23456
지불 금액: 100000
거스름돈: 76540(원단위 절사)
------------------------------
50,000 1장
10,000 2장
5,000 1장
1,000 1장
500 1개
100 0개
10 4개
------------------------------

2. 국어, 영어, 수학점수 입력 후 총점, 평균, 최고점수과목, 최저점수과목, 최고점수와 최저점수의 차이를 각각 출력해보자

korScore = int(input('국어점수 입력:'))
engScore = int(input('영어점수 입력:'))
matScore = int(input('수학점수 입력:'))

totalScore = korScore + engScore + matScore
avgScore = totalScore / 3

maxScore = korScore #가설 설정후 접근
maxSubject = '국어'
if engScore > maxScore :
    maxScore = engScore
    maxSubject = '영어'

if matScore > maxScore:
    maxScore = matScore
    maxSubject = '수학'

minScore = korScore
minSubject = '국어'
if engScore < minScore:
    minScore = engScore
    minSubject = '영어'

if matScore < minScore:
    minScore = matScore
    minSubject = '수학'

difScore = maxScore - minScore

print('총점: {}'.format(totalScore))
print('평균: %.2f' % avgScore)
print('-' * 30)
print('최고점수 과목(점수): {}({})'.format(maxSubject, maxScore))
print('최저점수 과목(점수): {}({})'.format(minSubject, minScore))
print('최고, 최저점수 차이: {}'.format(difScore))
print('-' * 30)

-->
국어점수 입력:55
영어점수 입력:90
수학점수 입력:84
총점: 229
평균: 76.33
------------------------------
최고점수 과목(점수): 영어(90)
최저점수 과목(점수): 국어(55)
최고, 최저점수 차이: 35
------------------------------

3. 시, 분, 초를 입력하면 초로 환산하는 프로그램을 만들어보자

hou = int(input('시간 입력: '))
min = int(input('분 입력: '))
sec = int(input('초 입력: '))

print('{}초'.format(format(hou * 60 * 60 + min * 60 + sec, ',')))
# format에 넣으면 문자가 됨. 연산은 안됨

4. 금액, 이율, 거치기간을 입력하면 복리계산하는 복리계산기 프로그램을 만들어보자

money = int(input('금액 입력: '))
rate = float(input('이율 입력: ')) #4.3%
term = int(input('기간입력: '))

targetMoney = money

for i in range(term):
    targetMoney += (targetMoney * rate * 0.01) #이자구하는 것. rate백분율 100으로 나눳

targetMoneyFormated = format(int(targetMoney), ',')

print('-' * 30)
print('이율: {}%'.format(rate))
print('원금: {}원'.format(format(money, ',')))
print('{}년 후 금액: {}원'.format(term, targetMoneyFormated))
print('-' * 30)

4. 고도가 60m 올라갈 때마다 기온이 0.8도 내려간다고 할 때 고도를 입력하면 기온이 출력되는 프로그램을 만들어보자(지면온도: 29도)

baseTemp = 29
step = 60
stepTemp = 0.8

height = int(input('고도 입력: '))

targetTemp = baseTemp - ((height // step) * 0.8)
if height % step != 0:
    targetTemp = targetTemp - stepTemp #targetTemp -= stepTemp

print('지면온도: {}도'.format(baseTemp))
print('고도 %dm의 기온: %.2f도' % (height, targetTemp))

-->
고도 입력: 130
지면온도: 29도
고도 130m의 기온: 26.60도

5. 197개의 빵과 152개의 우유를 17명의 학생에게 동일하게 나눠준다고 할 때, 한 명의 학생이 갖게 되는 빵과 우유의 개수를 구하고 남는 빵과 우유개수를 출력하자

bread = 197
milk = 152
studentCnt = 17

print('학생 한 명이 갖게 되는 빵 개수: {}'.format(bread // studentCnt))
print('학생 한 명이 갖게 되는 우유 개수: {}'.format(milk // studentCnt))

print('남는 빵 개수: {}'.format(bread % studentCnt))
print('남는 우유 개수: {}'.format(milk % studentCnt))

-->
학생 한 명이 갖게 되는 빵 개수: 11
학생 한 명이 갖게 되는 우유 개수: 8
남는 빵 개수: 10
남는 우유 개수: 16

6. 다음 내용을 참고해서 백신접종 대상자를 구분하기 위한 프로그램을 만들어보자

  • 의사코드(우리가 쓰는 자연어로 만든 코드)
    : 19세 이하 또는 65세 이상이면
    출생연도 끝자리에 따른 접종
    그렇지 않으면
    하반기 일정 확인
inputAge = int(input('나이 입력: '))

if inputAge <= 19 or inputAge >= 65:
    endNum = int(input('출생연도 끝자리 입력: '))
    if endNum == 1 or endNum == 6:
        print('월요일 접종 가능')
    if endNum == 2 or endNum == 7:
        print('화요일 접종 가능')
    if endNum == 3 or endNum == 8:
        print('수요일 접종 가능')
    if endNum == 4 or endNum == 9:
        print('목요일 접종 가능')
    if endNum == 5 or endNum == 0:
        print('금요일 접종 가능')
else:
    print('하반기 일정을 확인하세요')
    
-->
나이 입력: 19
출생연도 끝자리 입력: 9
목요일 접종 가능

7. 길이(mm)를 입력하면 inch로 환산하는 프로그램을 만들어보자(1mm = 0.039inch)

btInch = 0.039
lengthMM = int(input('길이(mm) 입력: '))
lengthInch = lengthMM * btInch

print('{}mm -> {}inch'.format(lengthMM, lengthInch))

-->
길이(mm) 입력: 18
18mm -> 0.702inch

0개의 댓글