다중조건문의이해 4 주차 _01 한동대학교 김경미교수
학습목표 조건절여러개쓰기연습하기 조건문오류찾기 코드의결과써보기
다중조건문 조건절여러개가표현되거나 조건절안에또다른조건절을기술하는경우 평균과과락을확인하는조건문 다양한예제로연습
두가지이상조건절한번에쓰기 0 < x < 10 if x > 0 and x < 10 : If 0 < x < 10: 0 > x or y < 0 if x < 0 or y < 0 : if a < 50 and b < 50 and c < 50 : if a >= 50 or b >= 50 or c >= 50 :
불리안형활용 a = 1 b = 2 c = 3 # Boolean data type. True or False a = True # And if a < b and a < c : print("a is less than b and c") # Non-exclusive or if a < b or a < c : print("a is less than either a or b (or both)") if a : print("a is true") else : print("a is false")
입력된월확인하기 Birthdate = input( 생년월일을입력하세요 : ") Year = Birthdate[ 0 : 4 ] Month = Birthdate[ 4 : 6 ] Day = Birthdate[ 6 : ] if Month < 01 or Month > 12 : print( 월이잘못입력되었습니다, 다시실행해주세요!) if Birthdate > 20190901 : print(" 나이를계산할수없습니다 ") else: age = 2019 - int(year) if Month > 09 : age = age - 1 print( 당신의나이는, age, 입니다 )
과락확인, or Korean = int(input(" 국어점수를입력하세요 : ")) English = int(input(" 영어점수를입력하세요 : ")) Math = int(input(" 수학점수를입력하세요 : ")) History = int(input(" 역사점수를입력하세요 : ")) if Korean < 50 or English < 50 or Math < 50 or History < 50 : print(" 과락입니다.")
합격확인, and Korean = int(input(" 국어점수를입력하세요 : ")) English = int(input(" 영어점수를입력하세요 : ")) Math = int(input(" 수학점수를입력하세요 : ")) History = int(input(" 역사점수를입력하세요 : ")) Mean = (Korean + English + Math + History) / 4 if Korean >= 50 and English >= 50 and Math >= 50 and History >= 50 and Mean >= 60 : print(" 합격입니다.")
연습문제 1, 오류찾기 1 #1 if num > 0 then print(num)
연습문제 1, 답안 1 #1 if num > 0 : print(num)
연습문제 1, 오류찾기 2 #2 if num > 0 : print(num) else print(num + 10)
연습문제 1, 답안 2 #2 if num > 0 : print(num) else : print(num + 10)
연습문제 1, 오류찾기 3 #3 if num = 10 : num = 1
연습문제 01, 답안 3 #3 if num == 10 : num = 1
연습문제 1, 오류찾기 #4 num = input( 정수를입력하세요 : ) s = num ** 2
연습문제 1, 답안 4 #4 num = input( 정수를입력하세요 : ) num = int(num) s = num ** 2
연습문제 1, 오류찾기 5 #5 grade= F if score >= 90 : grade = A if score >= 80 : grade = B if score >= 70 : grade = C if score >= 60 : grade = D
연습문제 1, 답안 5 #5 grade= F if score >= 90 : grade = A elif score >= 80 : grade = B elif score >= 70 : grade = C else : grade = D
연습문제 2, 결과써보기 #1 n = 1 m = -1 if n < -m : print(n) else : print(m)
연습문제 2, 답안 #1 n = 1 m = -1 if n < -m : print(n) else : print(m)
연습문제 2, 결과써보기 #2 n = 1 m = -1 l = 0 if n <= -m and m < l: print(n) elif m >= l : print(m) else : print(l)
연습문제 2, 답안 #2 n = 1 m = -1 l = 0 if n <= -m and m < l: print(n) elif m >= l : print(m) else : print(l)
연습문제 2, 결과써보기 #3 s1 = ab s2 = abc s3 = bc if s1 < s2 : print( s1;, s1) if s1 < s3 : print( s1;, s1) if s2 < s3 : print( s2;, s2)
연습문제 2, 답안 #3 s1 = ab s2 = abc s3 = bc if s1 < s2 : print( s1;, s1) if s1 < s3 : print( s1;, s1) if s2 < s3 : print( s2;, s2)
연습문제 3 국어, 영어, 수학시험성적을입력받는다 평균을구한다 3 과목중한과목이라도 50 점미만이면 과락 과락이없고, 평균이 60 점이상이면 합격, 아니면 불합격 이라고출력한다
연습문제 3 코드와결과 Korean = int(input(" 국어성적 : ")) English = int(input(" 영어성적 : ")) Math = int(input(" 수학성적 : ")) Mean = (Korean + English + Math) / 3 if Korean < 50 or English < 50 or Math < 50 : print(" 과락 ") elif Mean >= 60 : print(" 합격 ") else : print(" 불합격 ")
연습문제 4 정수 1,2,3,,12 를입력받는다 입력받은수가월이라고가정하고해당월에마지막날숫자를다음과같이출력한다 1, 3, 5, 7, 8, 10, 12 월은 31 일까지 2 월은 28 일또는 29 일까지 4, 6, 9, 11 월은 30 일까지 이외의정수이면 입력오류
연습문제 4, 코드 mon = int(input( 월을입력하세요 : ")) if mon==1 or mon==3 or mon==5 or mon==7 or mon==8 or mon==10 or mon==12 : print( 31 일까지 ") elif mon==2 : print( 28 일또는 29 일까지 ") elif mon==4 or mon==6 or mon==9 or mon==11 : print( 30 일까지 ") else : print( 입력오류 ")
연습문제 4, 코드리스트사용 mon = int(input(" 월을입력하세요 : ")) if mon in [1,3,5,7,8,10,12] : print("31 일까지 ") elif mon==2 : print("28 일또는 29 일까지 ") elif mon in [4,6,9,11] : print("30 일까지 ") else : print(" 입력오류 ")
연습하기 5 계절과실내온도를입력받는다 계절은겨울과여름만입력하도록한다 계절에따른실내온도가적정수준인지알려준다 겨울철실내적정온도는 20~22 도 여름철실내적정온도는 24~26 도 입력받은계절과실내온도를출력한다 적정온도일경우 적정온도입니다. 적정온도범위를벗어나는경우 적정온도가아닙니다. 를출력한다
연습하기 5, 코드 season = input(" 계절을입력하세요 : ") temp = int(input(" 실내온도를입력하세요 : ")) If season==" 겨울 ": if temp>=20 and temp<=22 : print(" 적정온도입니다 ") else: print(" 적정온도가아닙니다 ") elif season==" 여름 : if temp>=24 and temp<=26 : print(" 적정온도입니다 ") else: print(" 적정온도가아닙니다 ") else: print(" 계절은겨울과여름만입력할수있습니다 ")
숙제 연습문제 3, 4 코드와 실행결과캡쳐한사진을게시판에올려주세요!
강의요약 조건절여러개쓰기연습 두가지이상조건절을한번에사용 and, or, boolean 형활용 조건문오류찾기 조건문문법이해하고사용하기
사지선다 다음구문의오류를모두고르시오 num = 5 if num > 0 print(num) else print(num + 10) num = 5 if num > 0 print(num) else
사지선다답안 다음구문의오류를모두고르시오 num = 5 if num > 0 print(num) else print(num + 10) num = 5 if num > 0 print(num) else
사지선다 다음구문의의미를맞게설명한것은? if mon in [1,3,5,7,8,10,12] : 변수 mon 에저장된값이 1,3,5,7,8,10,12 중하나라면 변수 mon 에 1,3,5,7,8,10,12 를대입한다 1,3,5,7,8,10,12 를변수 mon 에서제외하면 변수 mon 은 [1,3,5,7,8,10,12] 이다
사지선다답안 다음구문의의미를맞게설명한것은? if mon in [1,3,5,7,8,10,12] : 변수 mon 에저장된값이 1,3,5,7,8,10,12 중하나라면 변수 mon 에 1,3,5,7,8,10,12 를대입한다 1,3,5,7,8,10,12 를변수 mon 에서제외하면 변수 mon 은 [1,3,5,7,8,10,12] 이다
감사합니다 4 주차 _01 다중조건문이해와활용