Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- deep learning
- elif
- python practice
- pytorch
- machine learning
- XOR
- GNN
- sentence embedding
- Transformer
- Classificaion
- abstraction
- neural net
- NLP
- Python
- Self-loop attention
- Set Transformer
- overfitting
- Attention
- word2vec
- sigmoid
Archives
- Today
- Total
Research Notes
[Python Practice] While/If Statement - Extract all numbers less than 100 that are multiples of 8 but not multiples of 12 본문
카테고리 없음
[Python Practice] While/If Statement - Extract all numbers less than 100 that are multiples of 8 but not multiples of 12
jiachoi 2023. 7. 3. 16:32<Question>
# while문과 if문을 활용하여 100이하의 자연수 중 8의 배수이지만, 12의 배수는 아닌 것을 모두 출력하세요. 실행하면 아래의 내용이 콘솔에 출력되어야 합니다.
8
16
32
40
56
64
80
88
<Answer>
i = 1
while i<=100 :
if i % 8 == 0 and i % 12 != 0 :
print(i)
i = i + 1
<Explanation>
콘솔에 출력되어야 할 내용이 작은수인 8부터 시작이므로 기본으로 지정해줘야 하는 변수는
i = 1부터 시작하도록 지정하였다.
반복해서 값이 출력되어야 하기 때문에 while의 조건문을 주어야 한다.
처음에 1~100까지를 출력하는 반복문을 만들어보면
i = 1
while i<=100:
print(i)
i = i+1
이런 형태로 만들어지게 된다.
여기에 8의 배수인데 12의 배수가 아닌 수만 출력하게 하는 조건을 주면 된다. (if문을 사용하여)
i = 1
while i<=100:
if i%8==0 and i%12 !=0:
print(i)
i = i+1
if 문에 있는 (i%8==0) 과 (i%12 !=0) 두 조건을 만족하게 하려면 and연산자로 묶어서 조건식을 주면 된다.

위 문제 결과 창