파이썬 16, 17

윤선민·2023년 11월 26일

파이썬

목록 보기
6/14

연습문제(연산자)

시, 분, 초 입력 -> 초 환산 프로그램

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

print('{}초'.format(format(hou * 60 * 60 + min * 60 + sec, ','))) 
#format함수를 이용해 ,를 넣으면 세자리씩 끊어줌
=========================================================================
시간 입력 : 9
분 입력 : 45
초 입력 : 51
35,151초

복리계산 프로그램

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

targetMoney = money

for i in range(term):
    targetMoney += targetMoney * rate * 0.01  #1년이 지났을 때 이자

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

print('-' * 30)
print('이율: {}%'.format(rate))
print('원금: {}원'.format(format(money,',')))
print('{}년 후 금액: {}원'.format(term, targetMoneyFormated))
print('-' * 30)
========================================================================
금액 입력: 1500000
이율 입력: 4.3
기간 입력: 5
------------------------------
이율: 4.3%
원금: 1,500,000원
5년 후 금액: 1,851,453원
------------------------------

고도입력, 기온 출력 프로그램

baseTemp = 29
step = 60
stepTemp = 0.8

height = int(input('고도 입력: '))
targetTemp = baseTemp - (height // step * 0.8)

if height % step != 0:
    targetTemp -= stepTemp

print('지면 온도: {}'.format(baseTemp))
print('고도 {}m의 기온: {}'.format(height, targetTemp))
================================================================
고도 입력: 720
지면 온도: 29
고도 720m의 기온: 19.4
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

백신 대상자 찾기

age = int(input('나이 입력: '))

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

inch 환산 프로그램

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

print('{}mm -> {}inch'.format(lengthMM, lengthInch))
============================================================
길이(mm) 입력: 18
18mm -> 0.702inch

교통 과속 위반 프로그램

speed= int(input('속도 입력: '))

if speed <= 50 :
    print('안전속도 준수!!')
else:
    print('안전속도 위반!! 과태료 50,000원 부과 대상!!!')
=====================================================
속도 입력: 59
안전속도 위반!! 과태료 50,000원 부과 대상!!!

문자요금결정 프로그램

text= input('메시지 입력: ')
textLength = len(text)

if textLength <= 50:
    print('SMS 발송!!')
    print('메시지 길이 : {}'.format(textLength))
    print('메시지 발송 요금 : 50원')
else:
    print('MMS 발송!!')
    print('메시지 길이 : {}'.format(textLength))
    print('메시지 발송 요금 : 100원')
=======================================================
메시지 입력: 안녕하세요
SMS 발송!!
메시지 길이 : 5
메시지 발송 요금 : 50원

과목별 총점, 편차 출력 프로그램

korAvg = 85; matAvg = 89; engAvg = 82; sciAvg = 75; hisAvg = 94;
totalAvg = korAvg + engAvg + matAvg + sciAvg + hisAvg
avgAvg = int(totalAvg / 5)

korScore = int(input('국어 점수 입력: '))
engScore = int(input('영어 점수 입력: '))
matScore = int(input('수학 점수 입력: '))
sciScore = int(input('과학 점수 입력: '))
hisScore = int(input('역사 점수 입력: '))

totalScore = korScore + engScore + matScore + sciScore + hisScore
avgScore = int(totalScore / 5)

korGap = korScore - korAvg
engGap = engScore - engAvg
matGap = matScore - matAvg
sciGap = sciScore - sciAvg
hisGap = hisScore - hisAvg

totalGap = totalScore - totalAvg
avgGap = avgScore - avgAvg

print('-'*70)
print('총점 : {}({}), 평균: {}({})'.format(totalScore, totalGap, avgScore, avgGap))
print('국어: {}({}), 영어: {}({}), 수학: {}({}), 과학: {}({}), 국사: {}({})'.format(
    korScore, korGap, engScore, engGap, matScore, matGap, sciScore, sciGap, hisScore, hisGap))
print('-'*70)
str = '+' if korGap > 0 else '-'
print('국어 편차 : {}({})'.format(str * abs(korGap), korGap))
str = '+' if engGap > 0 else '-'
print('영어 편차 : {}({})'.format(str * abs(engGap), engGap))
str = '+' if matGap > 0 else '-'
print('수학 편차 : {}({})'.format(str * abs(matGap), matGap))
str = '+' if sciGap > 0 else '-'
print('과학 편차 : {}({})'.format(str * abs(sciGap), sciGap))
str = '+' if hisGap > 0 else '-'
print('국사 편차 : {}({})'.format(str * abs(hisGap), hisGap))
str = '+' if totalGap > 0 else '-'
print('총점 편차 : {}({})'.format(str * abs(totalGap), totalGap))
str = '+' if avgGap > 0 else '-'
print('평균 편차 : {}({})'.format(str * abs(avgGap), avgGap))
print('-'*70)
===============================================================================
국어 점수 입력: 95
영어 점수 입력: 81
수학 점수 입력: 100
과학 점수 입력: 51
역사 점수 입력: 67
----------------------------------------------------------------------
총점 : 394(-31), 평균: 78(-7)
국어: 95(10), 영어: 81(-1), 수학: 100(11), 과학: 51(-24), 국사: 67(-27)
----------------------------------------------------------------------
국어 편차 : ++++++++++(10)
영어 편차 : -(-1)
수학 편차 : +++++++++++(11)
과학 편차 : ------------------------(-24)
국사 편차 : ---------------------------(-27)
총점 편차 : -------------------------------(-31)
평균 편차 : -------(-7)
----------------------------------------------------------------------

홀짝게임(난수 이용)

import random #난수를 발생시키는 모듈

comNum = random.randint(1, 2)
userSelect = int(input('홀/짝 선택: 1.홀 \t 2.짝\t 입력: '))

if comNum == 1 and userSelect ==1:
    print('빙고!! 홀수!!!')
elif comNum == 2 and userSelect ==2:
    print('빙고!! 짝수!!!')
elif comNum == 1 and userSelect ==2:
    print('실패!! 홀수!!!')
elif comNum == 2 and userSelect ==1:
    print('실패!! 짝수!!!')
================================================================
홀/짝 선택: 1.홀 	 2.짝	 입력: 1
실패!! 짝수!!!

가위바위보 게임(난수 이용)


comNum = random.randint(1, 3) #1,2,3중 난수 선택
userNum = int(input('가위, 바위, 보 선택:\t 1.가위 \t 2.바위\t 3.보\n 입력: '))

if (comNum == 1 and userNum == 2) or \
        (comNum == 2 and userNum == 3) or \
        (comNum == 3 and userNum == 1):
    print('컴퓨터: 패, 유저: 승')
elif comNum == userNum:
    print('무승부')
else:
    print('컴퓨터: 승. 유저: 패')

print('컴퓨터: {}, 유저: {}'.format(comNum,userNum))
===================================================================
위, 바위, 보 선택:	 1.가위 	 2.바위	 3.보
 입력: 2
컴퓨터: 패, 유저: 승
컴퓨터: 1, 유저: 2

이 글은 제로베이스 데이터 분석 스쿨의 강의 자료 일부를 발췌하여 작성되었습니다.

0개의 댓글