본문 바로가기

_Programming/Python

Basic.제어문

# if : 분기
weather = "맑음"
if weather == "비":
    print("우산을 챙기세요")
elif weather == "미세먼지":
    print("마스크를 챙기세요")
else:
    print("그냥 터덜터덜 나가세요")
 
 # input : 우리의 대답을 기다림.
weather = input("오늘 날씨는 어때요?")
if weather == "비" or weather == "눈":
    print("우산을 챙기세요")
elif weather == "미세먼지":
    print("마스크를 챙기세요")
else:
    print("그냥 터덜터덜 나가세요")

temp = int(input("기온은 어때요?"))
if 30 <= temp:
    print("쩌죽")
elif 10 <= temp and temp <30:
    print("아주 좋음. 산책가~")
elif 0 <= temp and temp < 10:
    print("겉옷 챙겨")
else:
    print("얼어죽")


# for : 반복문
print("대기번호 : 1")
print("대기번호 : 2")
print("대기번호 : 3")

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 (0~4)
    print("대기번호 : {0}".format(waiting_no))
for waiting_no in range(1,6): # 1, 2, 3, 4, 5 (1~5)
    print("대기번호 : {0}".format(waiting_no))

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:
        print("커피는 폐기처분됌.")

# customer = "아이언맨"
# index = 1
# while True:
#     print("{0}, 커피가 준비되었습니다. 호출{1}회".format(customer,index))
#     index += 1 # 무한루프(ctrl + c : 강제종료)
 
 #조건에 만족하면 계속 반복. 조건과 다르면 while문 종료.
customer = "아이언맨"
person = "Unknowm"
while person != customer : 
    print("{0}, 커피가 준비되었습니다.".format(customer))
    person = input("이름 뭐야?")


# continue : 다음으로 넘어가도 반복은 계속되야한다 & break : 반복을 끝내야한다.
absent = [2, 5] #결석
no_book = [7] # 책을 깜빡했음
for student in range(1, 11): # 1,2,3,4,5,6,7,8,9,10
    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을 붙이기로 함. -> 101, 102, 103, 104.
students = [1, 2, 3, 4, 5]
print(students)
students = [i+100 for i in students]
print(students)

 # 학생이름을 길이로 변환.
students = ["Iron man", "Thor", "I am Groot"]
students = [len(i) for i in students]
print(students)

 # 학생이름을 대문자로 변환.
students = ["Iron man", "Thor", "I am Groot"]
students = [i.upper() for i in students]
print(students)

# Quiz : 당신은 Cocoa 서비스를 이용하는 택시 기사님입니다.
# 50명의 승객과 매칭 기회가 있을 때, 총 탑승 승객 수를 구하는 프로그램을 작성하시오.
# 조건1 : 승객별 운행 소요 시간은 5분 ~ 50분 사이의 난수로 정해집니다.
# 조건2 :  당신은 소요시간 5분 ~ 15분 사이의 승객만 매칭해야 합니다.

#(출력문예제)
#[0] 1번째 손님 (소요시간 : 15분)
#[] 2번째 손님 (소요시간 : 50분)
#[0] 3번째 손님 (소요시간 : 5분)
# ...
#[50]번째 손님 (소요시간 : 16분)

# 총 탑승 승객 : 2분
# mine_sol
from random import *
isin = "탑승여부"
cnt = 0
for customer in range(1,51):
    time = randrange(5,51)
    if 5 <= time <= 15:
     isin = "O"
     print("[" +isin+ "] {0}번째 손님(소요시간 :{1}분)".format(customer,time))
     cnt += 1
    else :
     isin = " "
     print("[" +isin+ "] {0}번째 손님(소요시간 :{1}분)".format(customer,time))
print("총 탑승객 : {0}분".format(cnt))

 # other_sol
from random import *
cnt = 0 # 총 탑승객 수
for i in range(1,51): # 1 ~ 50 이라는 수(승객)
    time = randrange(5,51) # 5 ~ 51분 (소요시간) 
    if 5 <= time <= 15:
        print("[0] {0}번째 손님 (소요시간 : {1}분".format(i,time))
        cnt += 1
    else: 
        print("[ ] {0}번째 손님 (소요시간 : {1}분)".format(i,time))
    
print("총 탑승객 : {0}분".format(cnt))

 

range & randrange & randint 차이점. https://progolovego.tistory.com/126

 

 

 

출처: www.youtube.com/watch?v=kWiCuklohd

'_Programming > Python' 카테고리의 다른 글

Basic.입출력  (0) 2020.09.10
Basic.함수  (0) 2020.09.09
Basic.자료구조  (0) 2020.09.07
Basic.문자열처리  (0) 2020.09.04
Basic.연산자  (0) 2020.09.03