지금까지 배운 것: if문 else문 elif문, 다중if문, 중첩if문
학습목표: 여러 조건이 같은 행의 코드 안에 있을 때 확인하는 방법 깨우치기
참고자료: 롤러코스터 심화 알고리즘 / https://replit.com/@appbrewery/day-3-end#main.py
if condition1 & condition2 & condition3 : do this
else:
do this
= 논리 연산자를 사용해야 함.
A and B # and연산자로 결합시키려면 A,B 둘다 True여야 함. A 나 B 중 하나가 False면, 전체 거짓이란 결과가 나옴
C or D
not E
⇒ and연산자로 결합시키려면 A,B 둘다 True여야 함. A 나 B 중 하나가 False면, 전체 거짓이란 결과가 나옴
a = 12
a > 15
# 실행결과: False
a > 10
# 실행결과: True
a > 10 and a < 13
# 실행결과: True (두 조건 모두 True이므로)
a > 15 and a < 13
# 실행결과: False (앞 조건이 False이므로)
⇒ 여러 조건 중, 하나만 True면 된다.
⇒ A or B 에서, 둘중 1개가 True이거나 두 조건 모두 True 이면 실행결과 True 이다.
⇒ A, B 모두 False일 때만 실행결과 False가 나온다.
⇒ 조건에 반대되는 결과를 만듦.
조건이 True 라면 실행결과 False 를 출력하고
조건이 False라면, 실행결과 True를 출력함
a = 12
not a > 15
# 실행결과: True
롤러코스터 회사에서 중년 위기에 있는 모두에게 공짜 표를 주기로 했다
중년위기: 45~55세 사이에 일어남.
이 조건을 포함해서 다이어그램을 만들어보자
키 > 120 탑승가능
나이 < 12 --> +$5, 12<=나이<=18 --> $7, 나이 >=18 --> +$12, 45<=나이<=55 --> +$0
사진촬영 한다 --> +$3
height = int(input("Welcome to the rollercoaster! \nWhat is your height in cm? "))
age = int(input("What is your age? "))
photo = input("Do you want a photo taken? Y or N. ")
bill = 0
if height >= 120 :
print("You can ride the rollercoaster!")
if age < 12:
bill += 12
print(f"Child tickets are ${bill}")
elif age < 18 :
bill += 7
print(f"Youth tickets are ${bill}")
elif age >= 18 :
bill += 12
print(f"Adult tickets are ${bill}")
elif age >=45 and age <=55 :
bill += 0
print("You don't need to pay. tickets are free for you")
if photo== "Y":
bill += 3
print(f"Your final bill is ${bill}")
elif photo =="N":
bill += 0
print(f"Your final bill is ${bill}")
else :
print("Sorry, You are too small to ride.")
그 이유는 변수 정의를 최상단에 미리 다 끝내뒀기 때문임
if 변수 조건문 바로 윗줄에 변수를 정의해주자. (아래처럼)
그 이유는 elif age < 18: 조건문 바로다음 조건문이 elif age >= 18: 일 필요가 없이
el :age >=18: 의미니까, if age >=45 and <=55: 이렇게 표현하면 됀다.
즉 elif age >=45 and <=55: 으로 elif age < 18: 다음 조건문을 표현하면 됌
즉 아래와 같은 코드로 바꾸면 클리어~
print("Welcome to the rollercoaster!")
height = int(input("What is your height in cm? "))
bill = 0
if height >= 120:
print("You can ride the rollercoaster!")
age = int(input("What is your age? "))
if age < 12:
bill = 5
print("Child tickets are $5.")
elif age <= 18:
bill = 7
print("Youth tickets are $7.")
elif age >= 45 and age <= 55:
print("Everything is going to be ok. Have a free ride on us!")
else:
bill = 12
print("Adult tickets are $12.")
wants_photo = input("Do you want a photo taken? Y or N. ")
if wants_photo == "Y":
bill += 3
print(f"Your final bill is ${bill}")
else:
print("Sorry, you have to grow taller before you can ride.")
#python기초문법
#스타트위드유데미
#스터디윗미
#유데미
#유데미코리아