본문 바로가기
데이터분석/TIL

240610 TIL

by Freely_ 2024. 6. 10.
728x90
반응형

while 반복문

while 조건:

         실행할 코드

조건이 '참'인 동안 실행할 코드를 반복적으로 실행

'거짓'이 되면 종료

예시
count = 1
while count <= 5:
    print(count)
    count += 1

end=''

print 함수에 사용하면 출력 후 줄 바꿈 대신 지정된 문자열을 출력함

예시
print("Hello", end='')
print("World")

#HelloWorld

break

for 또는 while 반복문을 즉시 종료시킴

특정 조건이 만족되면 반복을 중단하고 루프를 벗어난다

예시
#while 루프
i = 0
while True:
    print(i)
    i += 1
    if i == 5:
        break

#for 루프
for i in range(10):
    if i == 5:
        break
    print(i)

둘 다 출력값은
0
1
2
3
4

[Python 과제] Lv3. 단어 맞추기 게임

import random

words = [
    "airplane",
    "apple",
    "arm",
    "bakery",
    "banana",
    "bank",
    "bean",
    "belt",
    "bicycle",
    "biography",
    "blackboard",
    "boat",
    "bowl",
    "broccoli",
    "bus",
    "car",
    "carrot",
    "chair",
    "cherry",
    "cinema",
    "class",
    "classroom",
    "cloud",
    "coat",
    "cucumber",
    "desk",
    "dictionary",
    "dress",
    "ear",
    "eye",
    "fog",
    "foot",
    "fork",
    "fruits",
    "hail",
    "hand",
    "head",
    "helicopter",
    "hospital",
    "ice",
    "jacket",
    "kettle",
    "knife",
    "leg",
    "lettuce",
    "library",
    "magazine",
    "mango",
    "melon",
    "motorcycle",
    "mouth",
    "newspaper",
    "nose",
    "notebook",
    "novel",
    "onion",
    "orange",
    "peach",
    "pharmacy",
    "pineapple",
    "plate",
    "pot",
    "potato",
    "rain",
    "shirt",
    "shoe",
    "shop",
    "sink",
    "skateboard",
    "ski",
    "skirt",
    "sky",
    "snow",
    "sock",
    "spinach",
    "spoon",
    "stationary",
    "stomach",
    "strawberry",
    "student",
    "sun",
    "supermarket",
    "sweater",
    "teacher",
    "thunderstorm",
    "tomato",
    "trousers",
    "truck",
    "vegetables",
    "vehicles",
    "watermelon",
    "wind"
]
word = random.choice(words)
letters = ''
life = 9

while True:
    succeed = True
    print()
    for i in word:
        if i in letters:
            print(i, end='')
        else:
            print('_', end='')
            succeed = False
    print()

    if succeed:
        print('축하합니다! 단어를 맞췄습니다!')
        break

    if life == 0:
        print(f'GAME OVER! 단어는 {word}였습니다!')
        break

    letter = input('A부터 Z까지 알파벳 중 하나를 입력하세요')
    if letter not in letters:
        letters += letter

    if letter in word:
        print('맞췄습니다! 다음 알파벳을 입력하세요')
    else:
        life -= 1
        print(f'포함되어 있지 않은 알파벳입니다. 남은 목숨:{life}')

SQL 연습하기

연습문제 1) 돈을 벌기 위해 일을 합시다!

id name position salary hire_date
1 르탄이 개발자 30000 2022-05-01
2 배캠이 PM 40000 2021-09-25
3 구구이 파트장 35000 2023-06-01
4 이션이 팀장 50000 2021-07-09

 

1. sparta_employees 테이블에서 모든 직원의 이름(name)과 직급(position)을 선택하는 쿼리를 작성해주세요.

select name, position

from sparta_employees

 

2. sparta_employees 테이블에서 중복 없이 모든 직급(position)을 선택하는 쿼리를 작성해주세요.

select distinct position

from sparta_employees

 

3. sparta_employees 테이블에서 연봉(salary)이 40000과 60000 사이인 직원들을 선택하는 쿼리를 작성해주세요.

select *

from sparta_employees

where salary between 40000 and 60000

 

4. sparta_employees 테이블에서 입사일(hire_date)이 2023년 1월 1일 이전인 모든 직원들을 선택하는 쿼리를 작성해주세요.

select *

from sparta_employees

where hire_date < '2023-01-01'


연습문제 2) 이제 좀 벌었으니 flex 한 번 해볼까요?!

id product_name price category
1 맥북 프로 1200 컴퓨터
2 다이슨 청소기 300 생활가전
3 갤럭시탭 600 컴퓨터
4 드롱기 커피머신 200 주방가전

 

5. products 테이블에서 제품 이름(product_name)과 가격(price)만을 선택하는 쿼리를 작성해주세요.

select product_name, price

from products

 

6. products 테이블에서 제품 이름에 '프로'가 포함된 모든 제품을 선택하는 쿼리를 작성해주세요.

select *

from products

where product_name like '%프로%'

 

7. products 테이블에서 제품 이름이 '갤'로 시작하는 모든 제품을 선택하는 쿼리를 작성해주세요.

select *

from products

where product_name like '갤%'

 

8. products 테이블에서 모든 제품을 구매하기 위해 필요한 돈을 계산하는 쿼리를 작성해주세요.

select sum(price) total_price

from products


단어 맞추기 게임 처음에 작성한 코드는 첫 알파벳만 넣어도 정답으로 나와서 구글링을 통해 처음부터 다시 작성

심지어 계속 오류가 발생하던 코드보다 더 쉽게 작성한 것 같다

728x90
반응형

'데이터분석 > TIL' 카테고리의 다른 글

240611 TIL  (0) 2024.06.11
240607 TIL  (0) 2024.06.07
240605 TIL  (0) 2024.06.05