조건문 실습 예제 에 대해 기술
저번에도 if 문 while 문에 대해 실습해보았다.
이번 실습도 비슷한 것이다
비교적 쉬운 코드니까 별 설명이 필요 없는 코드는 코드만 적고 넘어가겠다
score = int(input("enter your score:"))
if score >=90:
print('pass','\n' 'congretulations')
print('Thanks')
else는 조건이 없다. if가 참이 아닌 애들이 나온다. else 구문 안에서 출력을 해라 그렇게 말을 하는 것이다
else는 단독으로 쓰지 못한다. 당연한 말이다
your_mind = input("옷이 마음에 드는지 예, 아니요 로 입력하세요:")
if your_mind == '예':
print('축하합니다.')
else:
print('아쉽군요')
id = input("What if your ID? ")
if id == 'admin':
print("Hi admin!! please check lof file first")
else:
print("Welcome!", id )
이 예제문제를 대문자,소문자 관계없이 할 수 있다.
방법은] if 문 안에 .lower() 또는 .upper()를 넣어주면 된다
# 대소문자 상관없이 하기
id = input("What if your ID? ")
if id.lower() == 'admin':
print("Hi admin!! please check lof file first".lower())
else:
print("Welcome!", id )
나이와 키를 판별해서 입장여부를 판단하는 함
age = int(input("당신의 나이를 입력하세요:" ))
height = int(input("당신의 키를 입력하세요:" ))
if age >= 8 and height >=120:
print("입장하세요")
else:
print("다음에 오세요")
elif는 더 많은 조건을 주는 것이다
mind = input("마음에 드는 옷을 찾았나요? ")
if mind == '예':
print(mind, "축하합니다")
elif mind =='아니요':
print(mind, "아쉽군요")
else:
print("예 아니요 로 입력하세요")
score = int(input("What is you score: "))
if score >= 90:
print("Your grade is A")
elif score >= 80:
print("Your grade is B")
elif score >= 70:
print("Your grade is C")
elif score >= 60:
print("Your grade is D")
else:
print("Your grade is F")
age = int(input("몇 살이신가요?" ))
if age >= 65:
print("입장료는 8,000원입니다.")
elif 20 <= age < 65:
print("입장료는 10,000원입니다.")
elif age >= 7:
print("입장료는 7,000원입니다.")
else:
print("입장료는 없습니다")
입력하면 이대로 출력이 가능하니 그냥 쭉쭉 적었다
알파벳 자음 모음 판별하는 함수이다
A =['a','e','i','o','u','U''A','E','I','O']
juice = input("알파벳을 입력해주세요")
if juice in A:
print(juice, "는 모음입니다.")
else:
print(juice,"는 자음입니다.")
#A =['a','e','i','o','A','E','I','O']
juice = input("알파벳을 입력해주세요 ")
if juice in ['a','e','i','o','u','U','A','E','I','O']:
print(juice, "는 모음입니다.")
else:
print(juice,"는 자음입니다.")
frt = ['apple','melon','orange']
f = input('enter fruit: ')
if f in frt:
print('I like', f)
else:
print("I don't like",f)
# 다시 해보기
colors = ('blue', 'green','purple')
color = input("What if your favorite color? ")
if color.lower() in colors:
print("yes", color.title(), "is also my favorite color")
else:
print("no", color.title(), "is not my favorite color")
대문자이던 소문자이던 판별이 가능하게 코드 작성
c=""
if c:
print('if block')
else:
print('else block')
c=" "
if c:
print('if block')
else:
print('else block')
c=[]
if c:
print('if block')
else:
print('else block')
c=()
if c:
print('if block')
else:
print('else block')
c={}
if c:
print('if block')
else:
print('else block')
#빈 집합
c=set()
if c:
print('if block')
else:
print('else block')
# for문 뒤에는 무조건 컨테이너형이 나온다. 무조건 빈 것이 나온다.
모두 다 빈 것이 나온다
그리고 쉬운 아이디 비밀번호 입력하면 판별해주는 코드 작성
id = input("What is you ID? ")
if id.lower() == 'admin':
password = input("What if you Password?")
if password == '1234':
print("welcome admin")
else:
print("Wrong Password")
else:
print("You are not ADMiN")
조금 더 복잡한 키와 나이를 다 입력해야 통과할 수 있는 if else 함수
age = int(input("나이를 입력하세요: "))
if age >= 8:
height =int(input("키가 몇 인가요? "))
if height >= 120:
print("입장 가능합니다.")
else:
print("키가 120 이상이여야만 입장이 가능합니다.")
else:
print("나이가 8세 이상어어야 입장 가능합니다.")
여기까지 조건문 학습 예제 몇 가지 방법이었다