if문 까지 공부
This commit is contained in:
66
12. If.py
Normal file
66
12. If.py
Normal file
@@ -0,0 +1,66 @@
|
||||
# if문 개념
|
||||
temperature = 15
|
||||
|
||||
if temperature <= 10:
|
||||
print("자켓을 입는다")
|
||||
else:
|
||||
if temperature <= 15:
|
||||
print("긴팔을 입는다.")
|
||||
else:
|
||||
print("반팔을 입는다.")
|
||||
|
||||
# else if문은 elif
|
||||
if temperature <= 10:
|
||||
print("자켓을 입는다")
|
||||
elif temperature <= 15:
|
||||
print("긴팔을 입는다.")
|
||||
else:
|
||||
print("반팔을 입는다.")
|
||||
|
||||
|
||||
def print_grade(midterm_score, final_score):
|
||||
total = midterm_score + final_score
|
||||
if total >= 90:
|
||||
print('A')
|
||||
elif total >= 80:
|
||||
print('B')
|
||||
elif total >= 70:
|
||||
print('C')
|
||||
elif total >= 60:
|
||||
print('D')
|
||||
else:
|
||||
print('F')
|
||||
|
||||
# 코드를 쓰세요.
|
||||
|
||||
|
||||
# 테스트
|
||||
print_grade(40, 45)
|
||||
print_grade(20, 35)
|
||||
print_grade(30, 32)
|
||||
print_grade(50, 45)
|
||||
|
||||
# while문과 if문을 활용하여, 100 이하의 자연수 중 8의 배수이지만 12의 배수는 아닌 것을 모두 출력하세요.
|
||||
#
|
||||
# 예를 들어서 16은 8의 배수이지만 12의 배수가 아니니까 조건에 부합합니다. 하지만 48은 8의 배수이면서 12의 배수이기도 해서 조건에 부합하지 않습니다.
|
||||
#
|
||||
# 실행하면 콘솔에 아래와 같이 출력되어야 합니다.
|
||||
i = 1
|
||||
|
||||
while i <= 100:
|
||||
if i % 8 == 0 and i % 12 != 0:
|
||||
print(i)
|
||||
i += 1
|
||||
|
||||
# 10보다 작은 2 또는 3의 배수는 2, 3, 4, 6, 8, 9이며, 이들의 합은 32입니다.
|
||||
#
|
||||
# while문과 if문을 활용하여, 1,000보다 작은 자연수 중 2 또는 3의 배수의 합을 출력하는 프로그램을 써 보세요.
|
||||
i = 1
|
||||
sum = 0
|
||||
|
||||
while i <= 1000:
|
||||
if i % 2 == 0 or i % 3 == 0:
|
||||
sum += i
|
||||
i += 1
|
||||
|
||||
print(sum)
|
||||
Reference in New Issue
Block a user