Python 기초) 제어문과 반복문

2020. 12. 22. 22:16·공부하기/Python

1. 제어문

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

if score >= 90:
	print("{0}점은 상위권입니다.".format(score))
elif 70 <= score and score < 90:
	print("{0}점은 중상위권입니다.".format(score))
elif 50 <= score < 70:
	print("{0}점은 중위권입니다.".format(score))
else:
	print("{0}점은 하위권입니다.".format(score))

 

* input은 키보드의 입력 문자열을 엔터 전까지 받을 수 있다.

 

2. 반복문

 

* for in 구문

for number in range(1, 51):
    print("번호: {0}".format(number))

 

* 한줄 for in 연산

numbers = range(1, 11)
new_numbers = [i*10 for i in numbers]

print(new_numbers)
# [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

 

* 반복문과 제어문을 활용한 숫자 맞추기 프로그램

answer = 5
my_answer = 0
input_cnt = 0

while input_cnt < 10 : # 10번 반복
    
    my_answer = int(input("숫자를 입력하세요:"))
    
    if my_answer < answer :
        print("정답은 {0}보다 큽니다.".format(my_answer))
        input_cnt += 1
        continue # 아래 if문 스킵
    elif my_answer > answer :
        print("정답은 {0}보다 작습니다.".format(my_answer))
        input_cnt += 1
        continue # 아래 if문 스킵
    if my_answer == answer :
        print("정답입니다.")
        break # 반복문을 빠져나감

* while, continue, break은 다른 언어들의 일반적인 문법과 같다.

저작자표시 비영리 변경금지 (새창열림)

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

Python 기초) 입출력 기초  (1) 2021.01.05
Python 기초) 함수  (3) 2020.12.22
Python 기초) 튜플과 Set  (1) 2020.12.16
Python 기초) 리스트와 딕셔너리  (3) 2020.12.16
Python 기초) 문자열 포맷  (1) 2020.12.14
'공부하기/Python' 카테고리의 다른 글
  • Python 기초) 입출력 기초
  • Python 기초) 함수
  • Python 기초) 튜플과 Set
  • Python 기초) 리스트와 딕셔너리
hyunjicraft
hyunjicraft
모든 것을 기록하고 싶었지만 복잡하지 않은 것만 기록하게 된 블로그
    반응형
  • hyunjicraft
    개발망고발
    hyunjicraft
  • 전체
    오늘
    어제
    • 분류 전체보기
      • iOS
        • Swift
        • RxSwift
      • 공부하기
        • React
        • Python
        • 다른 PL
        • Figma
      • 스타트업
      • 글쓰기
        • 회고
  • 블로그 메뉴

    • 태그
  • 인기 글

  • 태그

    blender g
    react
    URLSessionDataTask
    생활코딩
    swift
    블렌더
    기술적도전
    RxSwift 비교
    스타트업경험
    알고리즘
    mvvm-c
    스타트업개발
    swift codable
    computer systems
    Communication Patterns
    RxSwift 이미지 다운로드
    setState()
    문자열 포맷
    비동기 프로그래밍
    Python
    ios system architecture
    중니어
    함수방식 컴포넌트
    맥에서 블렌더
    마스터 컴포넌트 연결 해제
    RxSwift image download
    피그마 인스턴스
    daummap
    게임런칭
    블렌더 g키
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
hyunjicraft
Python 기초) 제어문과 반복문
상단으로

티스토리툴바