학습 목표

  • 조건문 if, elif, else의 구조를 이해하고 사용할 수 있다.
  • 조건문을 이용해 분기되는 프로그램을 작성할 수 있다.
  • 사용자 입력을 기반으로 조건을 판별하는 실습을 할 수 있다.

조건문의 기본 구조 익히기

4.1 조건문이란?

조건문은 "만약 ~라면 ~하라"는 지시를 내리는 구조입니다.

score = 85
if score >= 90:
    print("A학점")
elif score >= 80:
    print("B학점")
else:
    print("C학점 이하")

if 구조 설명

  • if는 조건이 참일 때 실행됩니다.
  • elif는 그 다음 조건을 의미합니다.
  • else는 위 조건들이 모두 거짓일 때 실행됩니다.

4.2 if 예제 실습 1: 점수 등급 출력

문제: 시험 점수를 저장한 후 다음 조건대로 등급을 출력하세요.

  • 90점 이상 → A
  • 80점 이상 → B
  • 나머지 → C 이하

모범답안:

score = 88

if score >= 90:
    print("A 등급")
elif score >= 80:
    print("B 등급")
else:
    print("C 등급 이하")

해설:

  • 조건은 위에서 아래로 검사되며, 처음으로 참인 조건만 실행됩니다.
  • elif는 여러 개 쓸 수 있습니다.

4.3 중첩 if문

if문 안에 또 다른 if문을 넣을 수 있습니다.

score = 95
if score >= 90:
    print("A 등급")
    if score >= 95:
        print("축하합니다! 만점에 가까워요!")

실습 2: 중첩 조건 만들기

문제: 나이가 20세 이상이면 성인, 그리고 65세 이상이면 노인으로 출력하세요.

모범답안:

age = 70
if age >= 20:
    print("성인입니다.")
    if age >= 65:
        print("노인입니다.")

해설:

  • 중첩 조건은 조건 안에서 조건을 더 세밀하게 나눌 때 유용합니다.

4.4 if와 사용자 입력

input()으로 사용자에게 값을 받아올 수 있습니다. 기본적으로 문자열로 받아오므로, 숫자로 쓰려면 int()로 변환합니다.

age = int(input("나이를 입력하세요: "))
if age >= 18:
    print("성인입니다.")
else:
    print("미성년자입니다.")

실습 3: 사용자 입력 기반 조건문

문제: 사용자에게 점수를 입력받아 등급을 출력하는 프로그램을 만들어보세요.

모범답안:

score = int(input("점수를 입력하세요: "))

if score >= 90:
    print("A 등급")
elif score >= 80:
    print("B 등급")
elif score >= 70:
    print("C 등급")
else:
    print("F 등급")

해설:

  • input()은 항상 문자열로 받아오므로 정수형으로 변환 필요
  • 조건이 순서대로 검사되므로 높은 점수부터 검사해야 합니다

조건 응용 및 종합 실습

4.5 비교 연산 + 조건문 연습

실습 4: 숫자 비교 프로그램

문제: 사용자에게 숫자 두 개를 입력받아 더 큰 수를 출력하세요.

모범답안:

a = int(input("첫 번째 숫자: "))
b = int(input("두 번째 숫자: "))

if a > b:
    print("더 큰 수는:", a)
elif a < b:
    print("더 큰 수는:", b)
else:
    print("두 수는 같습니다.")

해설:

  • 조건문 안에서 숫자 비교 가능
  • ==을 통해 같음을 확인할 수 있음

4.6 종합 실습: 로그인 시스템

문제: 아이디와 비밀번호가 맞으면 “로그인 성공”, 아니면 “실패” 출력

  • 아이디: admin
  • 비밀번호: 1234

모범답안:

user_id = input("아이디를 입력하세요: ")
password = input("비밀번호를 입력하세요: ")

if user_id == "admin" and password == "1234":
    print("로그인 성공!")
else:
    print("로그인 실패!")

해설:

  • 여러 조건은 and, or로 조합할 수 있음
  • 문자열 비교도 ==를 사용함

마무리 퀴즈 & 정리

  1. if, elif, else의 차이점은?

    • if: 첫 조건 검사 / elif: 다음 조건 / else: 모두 아닐 경우
  2. 아래 코드의 결과는?

x = 10
y = 5
if x > y:
    print("x가 큽니다")
else:
    print("y가 같거나 큽니다")

x가 큽니다

  1. input()의 기본 반환값은? → 문자열(str)

다음 시간 예고

반복문 for, while을 배워서 컴퓨터에게 여러 번 일 시켜보자!


TechTinkerer's에서 더 알아보기

구독을 신청하면 최신 게시물을 이메일로 받아볼 수 있습니다.

댓글 남기기

  • Mastering CSS Grid and Flexbox for Responsive Web Design

    [Tutorial] · 2026-05-06 02:31 UTC Mastering CSS Grid and Flexbox for Responsive Web Design 💡 TL;DR Understand the fundamentals of CSS Grid and Flexbox, including layout models and responsive design, to build robust and adaptable web applications. 📚 Learning Objectives This tutorial introduces the basics of CSS Grid and Flexbox, covering layout models, box sizing,…

  • Complete Guide to HTML5 Forms and Validation

    [Tutorial] · 2026-05-06 01:29 UTC Complete Guide to HTML5 Forms and Validation 💡 TL;DR Master HTML5 forms and validation to build accessible and functional web applications. 📚 Learning Objectives This tutorial covers the basics of HTML5 forms, including validation, semantic attributes, and accessibility features. Learn how to create robust and user-friendly forms that meet modern…

  • Building a Simple Web Application with React

    [Tutorial] · 2026-05-06 00:27 UTC Building a Simple Web Application with React 💡 TL;DR Learn how to build a basic web application with React using components, state management, and routing. 📚 Learning Objectives This tutorial covers the basics of creating a simple web application with React, including components, state management, and routing. Learners will gain…

  • C++ Template Metaprogramming for Beginners

    [Tutorial] · 2026-05-05 23:24 UTC C++ Template Metaprogramming for Beginners 💡 TL;DR Master C++ template metaprogramming to perform compile-time calculations and optimize your code. 📚 Learning Objectives Learn how to master C++ template metaprogramming, a powerful technique for performing compile-time calculations. This guide covers its benefits, key concepts, and practical examples to help you get…

  • Understanding Pointers and Memory Management in C++

    [Tutorial] · 2026-04-30 05:10 UTC Understanding Pointers and Memory Management in C++ 💡 TL;DR Mastering pointers in C++ is crucial for efficient memory management and writing effective code. 📚 Learning Objectives This tutorial covers the fundamentals of pointers in C++, including declaration, initialization, and memory management. Students will learn how to effectively use pointers to…

← 뒤로

응답해 주셔서 감사합니다. ✨

TechTinkerer's에서 더 알아보기

지금 구독하여 계속 읽고 전체 아카이브에 액세스하세요.

계속 읽기

TechTinkerer's에서 더 알아보기

지금 구독하여 계속 읽고 전체 아카이브에 액세스하세요.

계속 읽기