IFP 겨울방학 스터디 | 코드업 기초 100제 (11)

유재우·2022년 2월 1일
0

IFP-겨울방학 스터디

목록 보기
31/38

11. 기초-조건/선택실행구조


  • 세 정수 a, b, c가 입력되었을 때, 짝수만 출력해보자.
    내 풀이)
a, b, c = map(int, input().split())
if a % 2 == 0:
    print(a)
if b % 2 == 0:
    print(b)
if c % 2 == 0:
    print(c)

강의풀이)

# 1
a, b, c = map(int, input().split())
if not a%2: print(a)
if not b%2: print(b)
if not c%2: print(c)
#2
a, b, c = map(int, input().split())
print( *(filter(lambda num: num%2 == 0, [a, b, c])) )

Tip1::
조건문 이용
조건문 배우러가기
Tip2::

  • filter() 이용
    조건문처럼 사용할 수 있지만 조건문은 아니다.
  • '*(asterisk)' 이용
  • 익명함수 lambda 이용
lambda 인자 : 반환값
ex) 
(lambda x : x+1)(10)

  • 세 정수 a, b, c가 입력되었을 때, 짝(even)/홀(odd)을 출력해보자.
    내 풀이)
a, b, c = map(int, input().split())
print('even', *(filter(lambda x: not x%2, [a, b, c])))
print('odd', *(filter(lambda x: x%2, [a, b, c])))

강의풀이)

# 1
a, b, c = map(int, input().split())
print( 'odd' if a%2 else 'even' )
print( b%2 and 'odd' or 'even' )
print( ['even', 'odd'][c%2] )
#2
a, b, c = map(int, input().split())
print( *map(lambda num: 'odd' if num%2 else 'even', [a, b, c]) )

  • 정수 1개가 입력되었을 때, 음(minus)/양(plus)과 짝(even)/홀(odd)을 출력해보자.
    입력
    -4
    출력
    minus
    even
    내 풀이)
a = int(input())
print(a<0 and 'minus' or 'plus')
print(a%2 and 'odd' or 'even' )

강의풀이)

num = int(input())
print( num>0 and 'plus' or 'minus' )
print( num%2 and 'odd' or 'even' )

  • 점수(정수, 0 ~ 100)를 입력받아 평가를 출력해보자.
    평가 기준
    점수 범위 : 평가
    90 ~ 100 : A
    70 ~ 89 : B
    40 ~ 69 : C
    0 ~ 39 : D
    로 평가되어야 한다.
    내 풀이)
score = int(input())
if 90<=score<=100:
    print('A')
elif 70<=score<=89:
    print('B')
elif 40<=score<=69:
    print('C')
elif 0<=score<=39:
    print('D')
else:
    print('0부터 100사이의 점수를 입력하세요')

강의풀이)

grade = int(input())
if grade >= 90:
  print('A+')
elif grade >= 70:
  print('B')
elif grade >= 40:
  print('C')
else:
  print('D')

Tip::
if-elif-else를 이용한다.


  • 평가를 문자(A, B, C, D, ...)로 입력받아 내용을 다르게 출력해보자.
    평가 내용
    [평가 : 내용]
    A : best!!!
    B : good!!
    C : run!
    D : slowly~
    나머지 문자들 : what?
    내 풀이)
grade = input()
if grade == 'A' or 'a':
    print('best!!!')
elif grade == 'B' or 'b':
    print('good!!')
elif grade == 'C' or 'c':
    print('run!')
elif grade == 'D' or 'd':
    print('slowly~')
else:
  print('what?')

강의풀이)

word = input()
if word == 'A':
  print('best!!!')
elif word == 'B':
  print('good!!')
elif word == 'C':
  print('run!')
elif word == 'D':
  print('slowly~')
else:
  print('what?')

  • 월이 입력될 때 계절 이름이 출력되도록 해보자.

    [월 : 계절 이름]
    12, 1, 2 : winter
    3, 4, 5 : spring
    6, 7, 8 : summer
    9, 10, 11 : fall
    내 풀이)
weather = int(input())
if weather == 12 or weather == 1 or weather == 2:
    print('winter')
elif weather == 3 or weather == 4 or weather == 5:
    print('spring')
elif weather == 6 or weather == 7 or weather == 8:
    print('summer')
elif weather == 9 or weather == 10 or weather == 11:
    print('fall')

강의풀이)

# 1
month = int(input())
if month==12 or month==1 or month==2:
  print('winter')
elif month==3 or month==4 or month==5:
  print('spring')
elif month==6 or month==7 or month==8:
  print('summer')
else:
  print('fall')
# 2
if month in [12,1,2]:
  print('winter')
elif month in [3,4,5]:
  print('spring')
elif month in [6,7,8]:
  print('summer')
else:
  print('fall')
profile
끝없이 탐구하는 iOS 개발자 유재우입니다!

0개의 댓글