[TIL] Python if문, for문, while문

이나현·2021년 6월 19일
0

python

목록 보기
3/10
post-thumbnail

if

if 조건:
실행명령

1) 날씨 if

weather = input("오늘 날씨는 어때요?") #input은 콘솔창에서 입력함으로써 반응 가능
if weather == "비" or weather == "눈":
    print("우산을 챙기세요")
elif weather == "미세먼지":
    print("마스크를 챙기세요")
else:
    print("준비물이 필요 없어요.")

2) 기온 if

temp = int(input("기온은 어때요?"))
if 30 <= temp:
    print("너무 더워요 나가지 마세요")
elif 10 <= temp and temp < 30:
    print("괜찮은 날씨에요")
elif 0 <= temp < 10:
    print("외투를 챙기세요")
else:
    print("너무 추워요. 나가지 마세요")

for문(반복문)

안의 조건이 끝날 때까지 반복!

randrange == range
for waitting_no in range(1, 6):  # 6미만까지
    print("대기번호: {0}".format(waitting_no))
starbucks = ["아이언맨", "토르", "그루트"]
for customer in starbucks:
    print("{0}, 커피가 준비되었습니다.".format(customer))

while문

조건이 나올때까지 while문 내의 명령 반복
1) 예제의 룰: 5번 이상 불러도 안오면 취소

customer = "토르"
index = 5
while index >= 1:
    print("{0}, 커피가 준비되었습니다. {1} 번 남았어요".format(customer, index))
    index -= 1
    if index == 0:
        print("커피는 폐기처분되었습니다.")

2) 계속 손님을 부름

customer = "아이언맨"
index = 1
while True:
    print("{0}, 커피가 준비되었습니다. 호출 {1}회".format(customer, index))
    index += 1

3) 손님을 부르고 이름 문의했을 때 맞으면 끝, 틀리면 다시 이름 문의

customer = "토르"
person = "Unknown"

while person != customer:
    print("{0}, 커피가 준비되었습니다.".format(customer))
    person = input("이름이 어떻게 되세요?")

input 값에 토르가 들어오면 끝남.

profile
technology blog

0개의 댓글