while 반복문은 무한반복 또는 조건을 활용해 반복할 때 혹은 시간을 기반으로 조건을 걸 때 사용한다.
+ 시간 관련 기능 가져오기
import time
# 유닉스 타임 구하기
time.time()
Unix Time = UTC 세계 표준시
⇒ 1970년 1월 1일 0시 0분 0초로부터 몇 초가 지났는지 정수로 나타낸 것
- break 키워드와 continue 키워드
# continue 키워드를 쓸 경우
for number in numbers:
if number < 10:
continue
#문장
#문장
#문장
# if else 구문을 사용할 경우
for number in numbers:
if number >= 10:
#문장
#문장
#문장
처음부터 반복에 조건을 걸고 사용할 때는 continue를 사용하는 게 좋다.
이후 처리의 들여쓰기를 하나 줄일 수 있기 때문.
p. 189, 4번 문제 교재 정답
max_value = 0
a = o
b = 0
for i in range(1, 100 // 2 + 1):
j = 100 - i
current = i * j
if max_value < current:
a = i
b = j
max_value = current
print("최대가 되는 경우: {} * {} = {}".format(a, b, max_value))
교재 다음 부분(문자열, 리스트, 딕셔너리와 관련된 기본 함수)에 나오는 개념들을 사용해서 다시 풀어보았다.
- list.append()와 max(list) 사용
max_value = 0
number_list = []
for i in range(1, 100 // 2 + 1)
j = 100 - i
current = i * j
my_list.append(current)
print("최대가 되는 경우: {} * {} = {}".format(i, 100-i, max(number_list))
- 리스트 내포 list comprehension (결과값만 출력)
max_value = 0
a = 0
b = 0
number_list = [i * (100-i) for i in range(1, 100 // 2 + 1)]
print(max(number_list))
리스트 내포에서 반복이나 리스트가 중첩됐을 때 입력하는 방법 참고
Python List comprehension double for loop | Example code
Simple do Outermost loop comes first, and then the inner loops subsequently to get List comprehension double for loop in Python.
tutorial.eyehunts.com
vscode에서 파이썬을 돌리다가 갑자기 처음보는 신택스 에러가 났는데,
file "<stdin>" line 1
Syntax Error: invalid syntax
터미널에서 exit() 을 입력해주니 해결되었다.
오류의 원인과 해결법에 관한 내용 참고
syntax error when using command line in python
I am a beginner to python and am at the moment having trouble using the command line. I have a script test.py (which only contains print("Hello.")), and it is located in the map C:\Python27. In my ...
stackoverflow.com
'컴퓨터 & 코딩 > Python' 카테고리의 다른 글
[혼공학습단8기][혼공파] 함수, 튜플과 람다 (0) | 2022.08.08 |
---|---|
[혼공학습단 8기][혼공파] 5주차 미션 (0) | 2022.08.08 |
[혼공학습단 8기][혼공파] 딕셔너리와 for 반복문 (3주차 보충) (0) | 2022.07.29 |
[혼공학습단 8기][혼공파] 리스트와 for 반복문 (3주차 보충) (0) | 2022.07.29 |
[혼공학습단 8기][혼공파] 4주차 미션 (0) | 2022.07.28 |