[Python] 기초 문제풀이(3)

강미진·2023년 4월 12일

조건문

  • 교통 과속 위반 프로그램
    • 시속 50km 이하 → 안전속도 준수 !!
    • 시속 50km 초과 → 안전속도 위반 !! 과태료 50,000원 부과 대상 !!
standard_velocity = 50
user_velocity = int(input('속도 입력 : '))
if user_velocity > standard_velocity :
    print('안전속도 위반 !! 과태료 50,000원 부과 대상 !!')
else :
    print('안전속도 준수 !!')
속도 입력 : 80
안전속도 위반 !! 과태료 50,000원 부과 대상 !!
  • 문자 메시지 길이에 따라 문자 요금이 결정되는 프로그램
    • 문자 길이 50이하 → SMS 발송(50원 부과)
    • 문자 길이 50초과 → MMS 발송(100원 부과)
user_message = input('메시지 입력 : ')
if len(user_message) > 50 :
    print('MMS 발송 !!')
    print('메시지 길이 : {}'.format(len(user_message)))
    print('메시지 발송 요금 : 100원')
else :
    print('SMS 발송 !!')
    print('메시지 길이 : {}'.format(len(user_message)))
    print('메시지 발송 요금 : 50원')
메시지 입력 : 안녕하세요. 반갑습니다. 이 문자 메세지는 확인을 위해 작성된 메세지입니다. 작성 확인이 완료되었습니다.
MMS 발송 !!
메시지 길이 : 58
메시지 발송 요금 : 100원
  • 국어, 영어, 수학, 과학, 국사 점수를 입력하면 총점을 비롯한 각종 데이터가 출력되는 프로그램
    • 과목별 점수를 입력하면 총점, 평균, 편차를 출력한다.
    • 과목별 평균 ( 국어 : 85, 영어 : 82, 수학 : 89, 과학 : 75, 국사 : 94)
    • 각종 편차 데이터는 막대그래프로 시각화
kor_avg = 85 ; eng_avg = 82 ; mat_avg = 89 ; science_avg = 75; history_avg = 94
total_avg = kor_avg + eng_avg + mat_avg + science_avg + history_avg
avg_avg = int(total_avg / 5)
# input_score -----------------------------------------------------
kor_score = int(input('국어 점수 : '))
eng_score = int(input('영어 점수 : '))
mat_score = int(input('수학 점수 : '))
sci_score = int(input('과학 점수 : '))
his_score = int(input('역사 점수 : '))
# total_score -----------------------------------------------------
total_score = kor_score + eng_score + mat_score + sci_score + his_score
score_avg = int(total_score / 5)
# specific_score --------------------------------------------------
korGap= kor_score - kor_avg
engGap= eng_score - eng_avg
matGap= mat_score - mat_avg
sciGap= sci_score - science_avg
hisGap= his_score - history_avg
# carculating  ----------------------------------------------------
totalGap = total_score  - total_avg
avgGap = score_avg - avg_avg
# print_score -----------------------------------------------------
print('총점 : {}({}) \t평균 : {}({})'.format(total_score, totalGap, score_avg, avgGap))
print('국어 : {}({}), 영어 : {}({}), 수학 : {}({}), 과학 : {}({}), 역사 : {}({})'.
      format(kor_score,korGap,eng_score,engGap,mat_score,matGap,sci_score,sciGap,his_score,hisGap))
# print_graph -----------------------------------------------------
print('-'*70)
str = '*' if korGap > 0 else '-'
print('국어 편차 : {}({})'.format(str * abs(korGap),korGap))  # 절댓값 함수 : abs()
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))
국어 점수 : 85
영어 점수 : 90
수학 점수 : 100
과학 점수 : 92
역사 점수 : 88
총점 : 455(30) 	평균 : 91(6)
국어 : 85(0), 영어 : 90(8), 수학 : 100(11), 과학 : 92(17), 역사 : 88(-6)
----------------------------------------------------------------------
국어 편차 : (0)
영어 편차 : ********(8)
수학 편차 : ***********(11)
과학 편차 : *****************(17)
역사 편차 : ------(-6)
총점 편차 : ******************************(30)
평균 편차 : ******(6)
  • 난수를 이용해서 홀/짝 게임
import random
com_num = random.randint(1, 2)
user_num = int(input('홀/짝 선택 : 1. 홀수 \t2. 짝수'))
if com_num == 1 and user_num ==1 :
    print('빙고 !! 홀수 !!')
elif com_num == 2 and user_num == 2 :
    print('빙고 !! 짝수 !!')
elif com_num == 1 and user_num == 2 :
    print('실패 !! 홀수 !!')
elif com_num == 2 and user_num == 1 :
    print('실패 !! 짝수 !!')
홀/짝 선택 : 1. 홀수 	2. 짝수 >> 2
실패 !! 홀수 !!
  • 난수를 이용해서 가위, 바위, 보 게임
import random
print('-'*30)
com_RCP = random.randint(1,3)
user_RCP = int(input('가위, 바위, 보 선택 : 1. 가위 \t2. 바위 \t3. 보'))
if (com_RCP ==1 and user_RCP == 2) or (com_RCP ==2 and user_RCP == 3) or (com_RCP ==3 and user_RCP == 1)  :
    print('컴퓨터 : 패, 유저 : 승!')
elif com_RCP == user_RCP :
    print('무승부')
else :
    print('컴퓨터 : 승, 유저 : 패')
가위, 바위, 보 선택 : 1. 가위 	2. 바위 	3. 보 >> 3
무승부
  • 상수도 요금 계산기를 만들어보자
# define ----------------------------------------------------
sector = int(input('업종 선택 (1.가정용 \t2.대중탕용 \t3.공업용) : '))
usage = int(input('사용량 입력 : '))
# print -----------------------------------------------------
print('='*20)
print('상수도 요금표')
print('-'*25)
# logic -----------------------------------------------------
if sector == 1:
    fee = usage * 540
elif sector == 2 :
    if usage<= 50 :
        fee = usage * 820
    elif usage <= 300 and usage >50 :
        fee = usage *1920
    else :
        fee = usage * 2400
elif sector == 3:
    if usage <= 500 :
        fee = 240 * usage
    else :
        fee = 470 * usage
fee_formed = format(fee, ',')
# print -----------------------------------------------------
print('사용량\t\t:\t\t요금')
print('{}\t\t:\t\t{}원'.format(usage, fee_formed))
print('='*20)
업종 선택 (1.가정용 	2.대중탕용 	3.공업용) : >> 3
사용량 입력 : 6500
================================
상수도 요금표
--------------------------------
사용량	 	:		요금
6500		:		3,055,000원
================================
```python
profile
g'day mate

0개의 댓글