if~else

elif 구문

# elif 사용 계절맞추기 210511_season.py
import datetime

now = datetime.datetime.now()

month = now.month

if 3 <= month <= 5 :
    print("{}월은 봄입니다.".format(month))
elif 6 <= month <= 8 :
    print("{}월은 여름입니다.".format(month))
elif 9 <= month <= 11 :
    print("{}월은 가을입니다.".format(month))
elif (month == 12) or (1 <= month <= 2) :
    print("{}월은 겨울입니다.".format(month))
else :
    print("{}월은 없는 달입니다.".format(month))

ex) 학점 유머 조건문으로 구현

210511_score.py

# 학점 유머 조건문으로 구현(1)
score = float(input("학점입력: "))

if score == 4.5 :
    print("신")
elif 4.2 <= score < 4.5 :
    print("교수님의 사랑")
elif 3.5 <= score < 4.2 :
    print("현 체제의 수호자")
elif 2.8 <= score < 3.5 :
    print("일반인")
elif 2.3 <= score < 2.8 :
    print("일탈을 꿈꾸는 소시민")
elif 1.75 <= score < 2.3 :
    print("오락 문화의 선구자")
elif 1.0 <= score < 1.75 :
    print("불가촉천민")
elif 0.5 <= score < 1.0 :
    print("자벌레")
elif 0 < score < 0.5 :
    print("플랑크톤")
elif score == 0 :
    print("시대를 앞서가는 혁명의 씨앗")
# 학점 유머 조건문으로 구현
score = float(input("학점입력: "))

# 학점 유머 조건문으로 구현(2)
if score >= 4.5 :
    print("신")
elif 4.2 <= score :
    print("교수님의 사랑")
elif 3.5 <= score :
    print("현 체제의 수호자")
elif 2.8 <= score :
    print("일반인")
elif 2.3 <= score :
    print("일탈을 꿈꾸는 소시민")
elif 1.75 <= score :
    print("오락 문화의 선구자")
elif 1.0 <= score :
    print("불가촉천민")
elif 0.5 <= score :
    print("자벌레")
elif 0 < score :
    print("플랑크톤")
else :
    print("시대를 앞서가는 혁명의 씨앗")

<aside> 💡 pass: 조건문 등에서 반드시 있어야 할 곳에 pass를 넣으면 통과된다.

</aside>

# pass 예시
score = float(input("학점입력: "))

if score >= 4.5 :
    pass
elif 4.2 <= score :
    pass
elif 3.5 <= score :
    print("현 체제의 수호자")

ex) P137 12간지 구별

210511_12cycle.py

# 년도에 따라 12간지 구분

str_input = input("태어난 해를 입력해 주세요> ")
i_birth_year = int(str_input) % 12

if i_birth_year == 0 :
    print("원숭이 띠입니다.")
elif i_birth_year == 1 :
    print("닭 띠입니다.")
elif i_birth_year == 2 :
    print("개 띠입니다.")
elif i_birth_year == 3 :
    print("돼지 띠입니다.")
elif i_birth_year == 4 :
    print("쥐 띠입니다.")
elif i_birth_year == 5 :
    print("소 띠입니다.")
elif i_birth_year == 6 :
    print("범 띠입니다.")
elif i_birth_year == 7 :
    print("토끼 띠입니다.")
elif i_birth_year == 8 :
    print("용 띠입니다.")
elif i_birth_year == 9 :
    print("뱀 띠입니다.")
elif i_birth_year == 10 :
    print("말 띠입니다.")
elif i_birth_year == 11 :
    print("양 띠입니다.")