and 구문을 사용하여 여러 조건문들을 연결하여 테스트 할 수 있다.
if (appearance == "준수") and (good_singer == True) and (dance == "Good"): print("훌륭한 아이돌이 될 수 있는 가능성이 높습니다!")
and 구문을 사용할 때 염두해야 할 점: and 구문을 사용하여 테스트 하는 모든 조건이 True 일때만 if 구문의 코드가 실행이 된다. 조건문들 중 하나라도 False 이면 실행이 안된다.
and 의 반대의 경우가 or 이다.
or 는 테스트 하는 조건들중 하나만 True 이여도 if 구문의 코드가 실행이 된다.
예시)
if test_score >= 400 or gold_medalist == True: print("서울대에 오신것을 환영합니다!") if age > 37 or age <= 19: print("죄송합니다 입장 불가합니다. ")
and 와 or 을 같이 사용할 수 도 있다.
예시)
if age > 19 and age < 30 or married == True and income < 100000000: print("WeCode 은행의 전세대출 우대자 이십니다")
가독성이 떨어진다. 그러므로 가독성을 높이기 위해서 ( ) 괄호를 사용하는 것이 좋다.
if (age > 19 and age < 30) or (married == True and income < 100000000): print("WeCode 은행의 전세대출 우대자 이십니다")
월(month)와 일(day), 이 2가지를 input 값으로 받았을때, 2019년의 해당 월과 일의 다음 날의 월과 일을 출력해주세요.
예를 들어, month 는 3이고 일은 31이면 2019년 3월 31일의 다음날은 4월 1일 임으로 다음과 같이 출력이 되면 됩니다 (월 과 일을 각각 다른 줄에 출력 해주세요).
4
1
My solution
month = int(input()) day = int(input()) def solution(month,day): if month in set([1,3,5,7,8,10,12]) and day == 31: print (month + 1) print (1) elif month in set([1,3,5,7,8,10,12]) and day != 31: print (month) print (day + 1) elif month in set([4,6,9,11]) and day == 30: print (month + 1) print (1) elif month in set([4,6,9,11]) and day != 30: print (month) print (day+1) elif month == 2 and day == 28: print (month + 1) print (1) elif month == 2 and day != 28: print (month) print (day + 1) solution(month,day) #조건 #1,3,5,7,8,10,12 는 31일까지 day 31이면 1 이외는 +1 #4,6,9,11 은 30일까지이면 day 30이면 1 이외는 +1 #월 = 2 and day = 28 이면 day 28이면 1 빼기 이외는 +1
Model solution
month = int(input()) day = int(input()) if ((day == 30) and (month == 4 or month == 6 or month == 9 or month == 11) or (day == 28) and (month == 2) or (day == 31)): month += 1 day = 1 else: day += 1 if month == 13: month = 1 print(month) print(day)