Python/문제

파이썬 과제

jjangdoll 2025. 1. 6. 10:19

3번

inventory_dict  = {'Apple': 90, 'Banana': 60, 'Orange': 80}
def maintain_inventory(data_dict, item, update):
  if item in data_dict:
    if data_dict[item] != update:
        data_dict[item] = update
        print('기존 품목', item, '을 업데이트합니다.')
        print('현재', item, '재고는', update, '% 있습니다.')
    else:
       print('현재', item, '재고는', update, '% 있습니다.')
  if item not in data_dict:
    data_dict[item] = update
    print('새로운 품목', item, '이 입고 되었습니다.')
    print('현재', item, '재고는', update, '% 있습니다.')
# 1. 기존 값 업데이트
maintain_inventory(inventory_dict, 'Apple', 50)
# 2. 새로운 품목이 들어옴
maintain_inventory(inventory_dict, 'Melon', 40)
'''
기존 품목 Apple 을 업데이트합니다.
현재 Apple 재고는 50 % 있습니다.
새로운 품목 Melon 이 입고 되었습니다.
현재 Melon 재고는 40 % 있습니다.
'''

- 이렇게까지는 구현을 했고, 이제 전체를 돌게 해서 전체적인 재고 조회를 하는 것을 구현해야하는데 어려움
- for을 이용하면 될 것 같아서 요리조리 시도하는데 에러만 한가득 뜸^^..

inventory_dict  = {'Apple': 90, 'Banana': 60, 'Orange': 80}
def maintain_inventory(data_dict, item, update):
  if item in data_dict:
    if data_dict[item] != update:
      data_dict[item] = update
      print('기존 품목', item, '을 업데이트합니다.')
    elif data_dict[item] == update:
      for item, update in inventory_dict.items():
        print('현재', item, '재고는', update, '% 있습니다.')
  if item not in data_dict:
    data_dict[item] = update
    print('새로운 품목', item, '이 입고 되었습니다.')
  else:
    print('현재', item, '재고는', update, '% 있습니다.')
# 1. 기존 값 업데이트
maintain_inventory(inventory_dict, 'Apple', 50)
# 2. 새로운 품목이 들어옴
maintain_inventory(inventory_dict, 'Melon', 40)
for item, update in inventory_dict.items():
  print('현재', item, '재고는', update, '% 있습니다.')
'''  
기존 품목 Apple 을 업데이트합니다.
현재 Apple 재고는 50 % 있습니다.
새로운 품목 Melon 이 입고 되었습니다.
현재 Apple 재고는 50 % 있습니다.
현재 Banana 재고는 60 % 있습니다.
현재 Orange 재고는 80 % 있습니다.
현재 Melon 재고는 40 % 있습니다.
'''

- 출력 중복되는 부분 빼고 밑에 for 추가해주니까 이렇게까지는 나오는데..
- 내 예상 : for이 큰 덩어리?로 한 번만 돌아서 밑에만 출력이 되는 것 같은 느낌
- 공통된 print('현재', item, '재고는', update, '% 있습니다.') 이 부분을 한 번에 처리해야 중복이 안 될 것 같음

inventory_dict  = {'Apple': 90, 'Banana': 60, 'Orange': 80}
def maintain_inventory(data_dict, item, update):
  if item in data_dict:
    if data_dict[item] != update:
      data_dict[item] = update
      print('기존 품목', item, '을 업데이트합니다.')
    else:
      print('현재', item, '재고는', update, '% 있습니다.')
  else:
    data_dict[item] = update
    print('새로운 품목', item, '이 입고 되었습니다.')
  for item, update in inventory_dict.items():
    print('현재', item, '재고는', update, '% 있습니다.')
# 1. 기존 값 업데이트
maintain_inventory(inventory_dict, 'Apple', 50)
# 2. 새로운 품목이 들어옴
maintain_inventory(inventory_dict, 'Melon', 40)

- 굳이 if를 두 번 쓰지 않아도 위에서 item이 data_dict에 있는 경우니까 else를 쓰면 없는 경우임 (바보)
- 변경된 경우만 출력하고 나머지는 생략하고 for로 묶었더니 나옴(바보2)

4번

- @ 누락 : 

len(email.split('@')) == 1
# @을 기준으로 쪼개지지 않았기 때문에 1이니까 @ 누락 추출 가능

- ID 누락 : 

(email.split('@'))[0] == ''
# @ 기준으로 쪼개고 첫번째 순서가 공복이면 아이디가 없는 것이므로 아이디 누락 추출 가능

- 잘못된 도메인 : 은 내일 다시.. 해볼 예정..

'Python > 문제' 카테고리의 다른 글

파이썬 | 한 번만 등장한 문자, 인덱스 바꾸기  (0) 2025.01.09
파이썬 | 합성수 찾기, 문자열 정렬하기(1)  (0) 2025.01.08
파이썬 과제  (0) 2025.01.08
파이썬 과제  (0) 2025.01.07