ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Python #4 if
    공부/Python 2021. 5. 12. 09:40

    if

    문법

    if 조건식 : 
        명령문
    

    예제

    score = 80
    
    if score >= 60 :
        print("%d 점" % score)
        print("합격입니다")
    

    -> 들여쓰기를 맞춰야한다

    if else

    문법

    if 조건식 :
        명령문
    else :
        명령문

    예제

    score = 80
    
    if score >= 60:
        print("합격입니다")
    else :
        print("불합격입니다")

    if elif

    문법

    if 조건식 :
        명령문
    elif :
        명령문
    else :
        명령문    
    

    elif는 여러번 사용할 수 있다

    예제

    score = 70
    
    if score >= 90:
        grade = "A"
    
    elif 80 <= score < 90:
        grade = "B"
    
    elif 70 <= score < 80:
        grade = "C"
    
    elif 60 <= score < 70:
        grade = "D"
    
    else :
        grade = "F"
    
    print("%d 점은 %s 등급입니다." % (score, grade))
    반응형

    '공부 > Python' 카테고리의 다른 글

    Python # 6 함수  (0) 2021.05.12
    Python #5 for  (0) 2021.05.12
    Python #3 연산자  (0) 2021.05.12
    Python #2 변수  (0) 2021.05.12
    Python #1 기초 문법의 이해  (0) 2021.05.12

    댓글

Designed by Tistory.