본문 바로가기

Python_Matter/[CodeUp]기초 100제

1070 : [기초-조건/선택실행구조] 월 입력받아 계절 출력하기

반응형

Q>

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


월 : 계절 이름
12, 1, 2 : winter
  3, 4, 5 : spring
  6, 7, 8 : summer
  9, 10, 11 : fall

참고
swtich( ).. case ... break; 제어문에서
break;를 제거하면 멈추지 않고 다음 명령이 실행되는 특성을 이용할 수 있다.

switch(a)
{
  ...
  case 3:
  case 4:
  case 5:
    printf("spring");
  break;
  ...
}
로 작성하면, 3, 4, 5가 입력되었을 때 모두 "spring"이 출력된다.

** 12, 1, 2 는 어떻게 처리해야 할지 여러 가지로 생각해 보아야 한다.

 

입력

월을 의미하는 1개의 정수가 입력된다.(1 ~ 12)

출력

계절 이름을 출력한다.

 

A>

n1 = int(input())

if n1 == 12 or n1 == 1 or n1 == 2:
    print('winter')
elif n1 == 3 or n1 == 4 or n1 == 5:
    print('spring')
elif n1 == 6 or n1 == 7 or n1 == 8:
    print('summer')
elif n1 == 9 or n1 == 10 or n1 == 11:
    print('fall')

 

A1>

a=input()

x=int(a)

if x==12 or x==1 or x==2 :
    print("winter")
elif x==3 or x==4 or x==5 :
    print("spring")
elif x==6 or x==7 or x==8 :
    print("summer")
elif x==9 or x==10 or x==11 :
    print("fall")

 

W>

if ~ elif 구문 / or 연산자

 

#>

admin, 2019년 10월 04일, http://codeup.kr

반응형