### 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 < 10:
print('외투를 챙기세요')
else:
print('너무 추워요.')
### 반복문 for
for waiting_no in range(1, 5): # 1 ~ 4
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("커피는 폐기처분되었습니다.")
customer1 = '아이언맨' # 무한루프 ,, ctrl+c 강제종료
index = 1
while True:
print("{0}, 커피가 준비 되었습니다. 호출 {1} 회".format(customer, index))
index += 1
customer = '토르'
person = 'Unknown'
while person != customer:
print("{0}, 커피가 준비 되었습니다.".format(customer))
person = input('이름이 어떻게 되세요? ')
### contine and 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~5인데 앞에 100을 붙인다. 101, 102, 103 ~ 105
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)
### 문제 5
'''50명의 승액과 매칭 기회가 있을때, 총 탑승 승객수를 구하는 프로그램을 작성하시오
조건1 : 승객별 운행 소요시간은 5~50분 사이의 난수
조건2 : 당신은 소요 시간 5~15분 승객만 매칭
(출력문 예제)
[o] 1번째 손님 (소요시간:15분)
[ ] 2번째 손님 (소요시간:50분)
[o] 3번째 손님 (소요시간:5분)
...
[ ] 50번째 손님 (소요시간:16분)
총 탑승 승객 : 2분 '''
# 나의 풀이
from random import *
customer = range(1,51)
customer = list(customer)
y=[]
for x in customer:
time = randrange(5,51)
if 5 <= time <= 15:
print("[o] {0}번째 손님 (소요시간:{1}분)".format(x, time))
y.append(x)
elif time > 15:
print("[ ] {0}번째 손님 (소요시간:{1}분)".format(x, time))
print("총 탐승 승객 : {0}분".format(len(y)))
# 정답
from random import *
cnt = 0
for i in range(1,51): # 50 명의 승객
time = randrange(5,51) # 5~50 소요 시간(분)
if 5 <= time <= 15:
print('[o] {0}번째 손님 (소요시간 : {1}분)'.format(i,time))
cnt += 1
else:
print('[ ] {0}번째 손님 (소요시간 : {1}분)'.format(i,time))
print('총 탑승 승객 : {0}분'.format(cnt))
* 출처 나도코딩 [파이썬 코딩 무료 강의 (기본편)]
파이썬 코딩 무료 강의 (기본편) - 6시간 뒤면 여러분도 개발자가 될 수 있어요 [나도코딩] (youtube.com)
'Python 뿌시기' 카테고리의 다른 글
[Python 문법 5] - 표준입출력, 출력포맷, 파일 입출력, pickle, with (7) | 2024.09.26 |
---|---|
[Python 문법 4] - 함수, 전달 값과 반환 값, 기본 값, 키워드 값, 가변인자, 지역변수와 전역변수 (3) | 2024.09.26 |
[Python 문법 2] - 리스트, 사전, 튜플, 세트, 자료 구조 변경 (0) | 2024.09.26 |
[Python 문법 1.2] - 문자열, 슬라이싱, 문자열 처리 함수, 포맷, 탈출문자 (0) | 2024.09.26 |
[Python 문법 1.1] - 연산, 수식, 숫자 처리 함수, 랜덤 함수 (0) | 2024.09.26 |