파이썬 연습 문제

hs0820·2023년 5월 7일

파이썬

목록 보기
5/16
post-thumbnail

파이썬 연습 문제


데이터

# 100살이 되는 해

myAge = int(input('나의 나이는?')) -> 33
thisAge = 100 - myAge
year = 2023
afterYear = year + thisAge
print('{}년 ({}년 후)에 100살!'.format(afterYear, thisAge))
-> 2090(67년 후)100살!

연산자

# 시, 분, 초를 입력하면 초로 환산하는 프로그램

hour = int(input('시간 입력 : ')) * 3600
month = int(input('분 입력 : ')) * 60
second = int(input('초 입력 : ')) * 1

totalSec = format((hour + month + second), ',')

print('{}초'.format(totalSec))
⬇
시간 입력 : 5
분 입력 : 30
초 입력 : 40
19,840# 길이를 입력하면 inch로 환산하는 프로그램(1mm = 0.039inch)

thisInch = 0.039
getMM = int(input('길이(mm) 입력 :'))

getInch = getMM * thisInch

print('{}mm -> {}Inch'.format(getMM, getInch))
⬇
길이(mm) 입력 :53
53mm -> 2.067Inch

조건문

# 시속 50km 이하 -> 안전속도 준수
# 시속 50km 초과 -> 안전속도 위반! 과태로 50,000원 부과 대상!

mySpeed = int(input('속도 입력 :'))
safeSpeed = 50

if mySpeed <= safeSpeed :
    print('안전속도 준수')
else :
    print('안전속도 위반! 과태로 50,000원 부과 대상!')
⬇
속도 입력 :47
안전속도 준수
속도 입력 :53
안전속도 위반! 과태로 50,000원 부과 대상!

useSelect = int(input('업종 선택(1.가정용  2.대중탕용  3.공업용):'))
usage = int(input('사용량 입력:'))
price = 0

if useSelect == 1 :
    price = 540
elif useSelect == 2 :
    if usage <= 50 :
        price = 820
    elif usage > 50 and usage <=300 :
        price = 1920
    else :
        price = 2400
elif useSelect == 3 :
    if usage <= 500:
        price = 240
    else:
        price = 470
else :
    print('잘못 선택하셨습니다.')

payed = price * usage
strPayed = format(payed,',')
print('=' * 20)
print('상수도 요금표')
print('-'*20)
print('사용량\t :\t 요금')
print('{}\t :\t {}원'.format(usage, strPayed))
print('=' * 20)
⬇
업종 선택(1.가정용  2.대중탕용  3.공업용):2
사용량 입력:310
====================
상수도 요금표
--------------------
사용량	 :	 요금
310	 :	 744,000====================


# PC에서 난수를 발생하면 사용자가 맞추는 게임을 만들어보자.
# • PC가 난수(1~1000)를 발생하면 사용자가 숫자(정수)를 입력한다.
# • 사용자가 난수를 맞추면 게임이 종료된다.
# • 만약, 못 맞추게 되면 난수와 사용자 숫자의 크고 작음을 출력한 후
# 사용자한테 다시 기회를 준다.
# • 최종적으로 사용자가 시도한 횟수를 출력한다.

import random
rNum = random.randint(1, 1000)
count = 0
gameFlag = True

while gameFlag :
    count += 1
    cNum = int(input('숫자를 입력하세요.'))

    if cNum == rNum :
        print('빙고')
        gameFlag = False
    elif cNum < rNum :
        print('난수 보다 작다.')
    else :
        print('난수 보다 크다.')

print('난수 {}, 횟수 {}'.format(rNum, count))
⬇
숫자를 입력하세요.510
난수 보다 작다.
숫자를 입력하세요.515
난수 보다 크다.
숫자를 입력하세요.513
난수 보다 크다.
숫자를 입력하세요.512
난수 보다 크다.
숫자를 입력하세요.511
빙고
난수 511, 횟수 13

반복문

# 1부터 100까지 정수 중 십의자리와 일의자리에 대해 각각 홀/짝수를 구분하는 프로그램

for i in range(1, 101) :
    if i <= 9 :
        if i % 2 == 0 :
            print('({}) : 짝수'.format(i))
        else :
            print('({}) : 홀수'.format(i))
    else :
        num10 = i // 10
        num1 = i % 10

        even = '짝수'
        odd = '홀수'

        result10 = ''
        reult1 = ''

        if num10 % 2 == 0 and num1 % 2 == 0 :
            result10 = even
            reult1 = even
        elif num10 % 2 == 0 :
            result10 = even
            reult1 = odd
        elif num1 % 2 == 0 :
            result10 = odd
            reult1 = even
        else :
            result10 = odd
            reult1 = odd
        if num1 == 0:
            reult1 = '0'
            
        print('({}) 십의 자리 : {}, 일의 자리 : {}'.format(i, result10, reult1))(1) : 홀수
(2) : 짝수
(10) 십의 자리 : 홀수, 일의 자리 : 0
(11) 십의 자리 : 홀수, 일의 자리 : 홀수
(59) 십의 자리 : 홀수, 일의 자리 : 홀수
(60) 십의 자리 : 짝수, 일의 자리 : 0
(61) 십의 자리 : 짝수, 일의 자리 : 홀수
(62) 십의 자리 : 짝수, 일의 자리 : 짝수
(98) 십의 자리 : 홀수, 일의 자리 : 짝수
(99) 십의 자리 : 홀수, 일의 자리 : 홀수
(100) 십의 자리 : 짝수, 일의 자리 : 0
for i in range(5, 0, -1) :
    for j in range(5 - i) :
        print(space, end='')
    for h in range(i) :
        print('*', end='')
    print()

# *****
#  ****
#   ***
#    **
#     *

for i in range(1, 10) :
    if i <= 5 :
        for j in range(i) :
            print('*', end='')
    else :
        for j in range(10 - i) :
            print('*', end='')
    print()

# *
# **
# ***
# ****
# *****
# ****
# ***
# **
# *

풀어 본 문제들 중 몇가지만 가져왔다.
이렇게 차근차근 기초부터 문제를 통해 배워나간다면
풀 수 있다는 자신감 또한 점점 커질 것 같다.

profile
개발 스터디 노트

0개의 댓글