Chapter 03 조건문


My Image

1. 불 자료형과 if 조건문(108`125)

Boolean : True/False

비교연산자 : ==, !=, <, >, <=, >= 논리연산자 : not, and, or

print(not True)
print(True and True)
print(False and True)
print(True & True)
print(True & False)
print(False or False)
print(True or False)
print(False | False)
print(False | True)

False

True

False

True

False

False

True

False

True

if 조건문 : 조건분기

날짜시간 활용하기

import datetime

now = datetume.datetime.now()

print(now.year, "년")
print(now.month, "월")
print(now.day, "일")
print(now.hour, "시")
print(now.minute, "분")
print(now.second, "초")

2021 년

11 월

23 일

2 시

5 분

59 초

PS D:\■■■■■■■_■FFF\■\python> & C:/■■■■■/■■■■/■■■■■/■■FFF■■/FFF■■■■■■■■/python.exe d:/basic_file/s/python/FFF.py
2021년 11월 23일 2시 9분 45초
PS D:\■■■■■■■_■FFF\■\python> 

if 문을 사용해서 오전/오후나 계절을 알려줄수 있다.

 

2. if~else와 elif 구문(p126~137)

if (조건식): #처음조건

elif (조건식): #다른 조건, if의 조건식이 안맞는 경우에 확인

else: # if와 elif의 조건이 안맞을때

pass 키워드 : 미구현 상태이거나 아무것도 안하는 상태에서

Indentation Error를 발생시키지 않게하기위해 사용.

※raise NotImplement Error:구현하지 않은 부분을 에러로 발생시킴

num=1

if num:
   raise NotImpLementedError
else:
  print("why?")
if 로 들어가게되면 강제로 에러를 발생시킨다.
Traceback (most recent call last):
File "d:\■■■■■_fFF\■\python\■■.py", line 4, in <module>
raise NotImpLementedError
NameError: name 'NotImpLementedError' is not defined 
해당챕터 미션
str_input=("which year were you born in?")

by=int(str_input)%12

if by==0:
   print("monkey")
elif by==1:
   print("chicken")
elif by==2:
  print("dog")
elif by==3:
   print("pig")
elif by==4:
   print("mouse")
elif by==5:
   print("cow")
elif by==6:
   print("tiger")
elif by==7:
   print("rabbit")
elif by==8:
   print("dragon")
elif by==9:
   print("snake")
elif by==10:
   print("horse")
elif by==11:
   print("sheep")

결과 :

which year were you born in?8
dragon
PS D:\■■■■■_fFF\■\p■th■>

선택 미션: else 구문과 elif 구문 정리한 내용 포스팅하기

C에서 이미 많이 다뤘던 내용이어서 모르는 것들 위주로 이전

게시글에 올렸다