Python/공부

기본편: 제어문(if, for, while)

sim7688 2022. 5. 24. 10:33
728x90
반응형
## if문(조건문) ***** 

# 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")
# print("대기번호 : 4")

# for waiting_no in [0, 1, 3, 4]:
#     print("대기번호 : {0}".format(waiting_no))

# 범위 : randrange()
# for waiting_no in range(1, 6):  # 0, 1, 2, 3, 4
#     print("대기번호 : {0}".format(waiting_no))

# starbuks = ["아이언맨", "토르", "아이엠 그루트"]
# for customer in starbuks:
#     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 


# customer = "토르"
# person = "Unknown"

# while person != customer :  # 조건에 만족할때까지 계속 이 안에있는 문장을 반복함
#     print("{0}, 커피가 준비 되었습니다.".format(customer))
#     person = input("이름이 어떻게 되세요?")


## contiue & 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   # contiue : 아래에 있는 문장을 더이상 실행하지 않고 다음 반복으로 계속 진행시킴
#     elif student in no_book:
#         print("오늘 수업 여기까지. {0}는 교무실로 따라와".format(student))
#         break      # break : 그 뒤에 내용이 있든 없든간에 반복문을 탈출하는 것 
#     print("{0}, 책 읽어봐".format(student))


## 한줄로 끝내는 for문
# 출석번호가 1,2,3,4 앞에 100을 붙이기로 함 -> 101, 102, 103, 104 
# students = [1,2,3,4]
# print(students)
# students = [i+100 for i in students]    
# print(students)

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

# # 학생 이름을 대문자(upper)로 변환
# 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분 사이의 승객만 매칭해야 합니다.

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

# 총 탑승 승객 : 2분 

# for customer in range(1,51):
#     print("대기승객 {0}".format(customer))

# from random import randrange

# bun = int(randrange(5,50))
# print(bun)    

# for customer in range(1,51):
#     # print("대기승객 {0}".format(customer))
#     if customer in bun <= 50:
#         break


# 풀이방법 
from random import *
cnt = 0 # 총 탑승 승객 수 
for i in range(1,51): # 1 ~ 50 이라는 수 (승객)
    time = randrange(5,51) # 5분 ~ 50분 소요시간
    if 5 <= time <= 15:  # 5분 ~ 15분 이내의 손님, 탑승 승객 수 증가 처리 
        print("[o] {0}번째 손님 (소요시간 : {1}분)".format(i, time))
        cnt += 1 
    else:  # 매칭 실패한 경우
        print("[ ] {0}번째 손님 (소요시간 : {1}분)".format(i, time))    

print("총 탑승 승객 : {0}분".format(cnt))

 

참고 

- 나도코딩(기본편)

728x90
반응형