[Python] 조건문, 반복문

쁘루뿅·2022년 3월 27일
0

파이썬

목록 보기
7/8

1. 반복문

#분기
#weather = "미세먼지"
#값 입력받기
weather = input("오늘 날씨 어때요? :")
"""
    ---- 형식 ----
    if 조건:
        실행 명령문
"""
if weather == "비":
    print("우산을 챙겨라")
elif weather == "미세먼지":    #다른 조건일 때
    print("마스크를 챙겨라")
else:                         #위의 모든 조건이 아닐 때
    print("오늘은 뭘 들고갈 필요가 없네요")



#조건이 다수있을 때
# or : 조건1 or 조건2
# 조건1 또는 조건2를 만족할 경우
weather2 = input("오늘 날씨 어때요?2 : ")
if weather2 == "비" or weather2 == "눈":
    print("우산을 챙겨라")

# and : 조건1 and 조건2
# 조건1과 조건2를 모두 만족할 경우
temp = int(input("기온이 어때요? :"))  #입력값을 int type으로 변환
if 30 <= temp:
    print("너무 더워요. 나가지 마세요.")
elif 10 <= temp and temp < 30:
    print("괜찮은 날씨예요") 
#elif 0 <= temp and temp < 10:
elif 0 <= temp < 10:   #위 조건을 왼쪽과 같이 합쳐쓸 수 있다
    print("외투를 챙기세요")
else:
    print("너무 추워요. 나가지마세요")

2. 조건문

#for : 반복문
#특정 값을 넣을 경우
for waiting_no in [0,1,2,3,4]:
    print("대기번호 : {0}".format(waiting_no))
# [] 의 값을 순서대로 반복 출력

#randrange()
for waiting_no in range(5): #0,1,2,3,4
    print("대기번호 : {0}".format(waiting_no))
# range 범위의 값을 순서대로 반복 출력

starbucks = ["쿠로","아도니스","나기사","린네"]
for customer in starbucks: #리스트 안의 멤버를 한명씩 호출
    print("{0}님, 커피 준비되었습니다.".format(customer))
#while 반복문 :조건만족할 때까지 반복
customer = "쿠로"
index = 5 
while index >= 1:
    print("{0}님, 커피가 준비되었습니다. {1}번 남았습니다.".format(customer,index))
    index -= 1
    if index == 0: #이 조건 만족 시 while stop
        print("커피가 폐기처분되었습니다.")

"""
customer = "나기사"
index = 5 
while True:
    print("{0}님, 커피가 준비되었습니다. {1}번 남았습니다.".format(customer,index))
    index += 1
    #무한루프에 빠짐
"""

customer = "아도니스"
person = "unknown"

while person != customer :
    print("{0}님, 커피가 준비되었습니다. ".format(customer))
    person = input("이름이 어떻게 되세요?")
#continue & break : 반복문에서 사용
absent = [2,5] #결석
no_book = [7] #책없음
for student in range(1,11):
    if student in absent:
        continue #계속하기
    elif student in no_book :
        print("오늘 수업 여기까지.{0}는 교무실오렴".format(student))
        break #반복문 종료
    print("{0},책읽어라.".format(student))

#한줄로 끝내는 for
#출석번호가 1,2,3,4... 앞에 100 붙이기로함
students  = [1,2,3,4,5]
print(students)
students = [i+100 for i in students]
print(students)
profile
개발지겸 엔지니어 oO(새 기기를 가지고 싶다)

0개의 댓글