파이썬/기본

[파이썬][응용]음악 추천 프로그램 - if 제어문 공부

caramel-bottle 2023. 9. 6.

프로그램 설명

IF 제어문을 사용하여 간단한 음악 추천 프로그램을 만들어 보았다.

급하게 만들었기에 어떤 경우엔 이상한 출력이나 오류가 발생할 수 있다.

추천해줄 음악 리스트는 딕셔너리에 미리 저장하였다.

key값은 연령대로 하였고 value에 각각의 항목에 맞는 음악을 넣었다.

 

입력 프롬프트와 딕셔너리 선언 부분.

print("음악을 추천해드립니다.")

music_list_yes = {40:["이문세 - 깊은 밤을 날아서", "김광석 - 바람이 불어오는 곳", "양희은 - 사랑 그 쓸쓸함에 대하여", "이규석 - 기차와 소나모", "린킨파크 - numbs"],
                  30:["키네틱플로우 - 몽환의 숲", "ABBA - Mamma mia", "야다 - 이미 슬픈 사랑", "이문세 - 그대 나를 보면", "Billy Joel - Piano man"],
                  20:["뉴진스 - Hype Boy", "태연 - Happy", "정국 - seven", "아이브 - IAM", "아이브 - love dive"],
                  10:["김수영 - 좋아보여", "김수영 - 사랑하자", "김수영 - 비워내려고합니다", "김수영 - 꿈", "김수영 - 학교 앞 정문"]}



music_list_no = {40:["오승근 - 내 나이가 어때서", "쿨 - 아로하", "김광진 - 편지", "김광석 - 일어나", "김경호 - 금지된사랑"],
                 30:["김연자 - 아모르파티", "악뮤 - 200%", "야다 - 그래서 그대는", "임재범 - 너를위해", "코요태 우리의 꿈"],
                 20:["뉴진스 - OMG", "제이래빗 - Happy things", "뉴지스 - super shy", "BTS - butter", "세븐틴 - 손오공"],
                 10:["김수영 - 모르겠다", "김수영 - 내가 없어도", "김수영 - 달이 나만 따라오네", "김수영 - 갑자기 생각이 난 건데", "김수영 - notitle"]}

select = int(input("""지금 기분이 어떠신가요?
1. 잘 모르겠다.
2. 행복하다.
3. 슬프다.
4. 답답하다.
5. 따분하다.
"""))

 

기분 항목을 입력하면 다음 코드가 실행된다.

연령대를 고려하여 음악을 추천해주기 위해 나이를 입력받는다.

입력한 나이에 해당하는 범위의 if문이 실행된다.

또 한번의 질문 프롬프트로 (yes or no)를 입력받는다.

(여기서의 질문은 아무 의미 없다. 의미 있는 질문으로 바꾸고 추천해줄 음악도 관련지으면 더욱 멋진 프로그램이 될 것이다.)

select == 1 외의 경우에도 동일하다.

if select == 1: # 잘 모르겠다.
  age = int(input("나이를 입력하시오."))

  if age > 40:
    ans = input("배고프신가요? ( yes or no ) ")
    if ans == 'yes':
      msg = music_list_yes[40][select-1]
      print(msg)
    elif ans == 'no':
      msg = music_list_no[40][select-1]
      print(msg)
    else:
      print("잘못입력하셨습니다.")

  elif age > 30:
    ans = input("배고프신가요? ( yes or no ) ")
    if ans == 'yes':
      msg = music_list_yes[30][select-1]
      print(msg)
    elif ans == 'no':
      msg = music_list_no[30][select-1]
      print(msg)
    else:
      print("잘못입력하셨습니다.")

  elif age > 20:
    ans = input("배고프신가요? ( yes or no ) ")
    if ans == 'yes':
      msg = music_list_yes[20][select-1]
      print(msg)
    elif ans == 'no':
      msg = music_list_no[20][select-1]
      print(msg)
    else:
      print("잘못입력하셨습니다.")

  elif age > 10:
    print("양심이 없으시군요.")
    msg = music_list_yes[10][select-1]
    print(msg)

  else:
    print("나이를 잘못 입력하셨습니다.")

 

전체 코드

print("음악을 추천해드립니다.")

music_list_yes = {40:["이문세 - 깊은 밤을 날아서", "김광석 - 바람이 불어오는 곳", "양희은 - 사랑 그 쓸쓸함에 대하여", "이규석 - 기차와 소나모", "린킨파크 - numbs"],
                  30:["키네틱플로우 - 몽환의 숲", "ABBA - Mamma mia", "야다 - 이미 슬픈 사랑", "이문세 - 그대 나를 보면", "Billy Joel - Piano man"],
                  20:["뉴진스 - Hype Boy", "태연 - Happy", "정국 - seven", "아이브 - IAM", "아이브 - love dive"],
                  10:["김수영 - 좋아보여", "김수영 - 사랑하자", "김수영 - 비워내려고합니다", "김수영 - 꿈", "김수영 - 학교 앞 정문"]}



music_list_no = {40:["오승근 - 내 나이가 어때서", "쿨 - 아로하", "김광진 - 편지", "김광석 - 일어나", "김경호 - 금지된사랑"],
                 30:["김연자 - 아모르파티", "악뮤 - 200%", "야다 - 그래서 그대는", "임재범 - 너를위해", "코요태 우리의 꿈"],
                 20:["뉴진스 - OMG", "제이래빗 - Happy things", "뉴지스 - super shy", "BTS - butter", "세븐틴 - 손오공"],
                 10:["김수영 - 모르겠다", "김수영 - 내가 없어도", "김수영 - 달이 나만 따라오네", "김수영 - 갑자기 생각이 난 건데", "김수영 - notitle"]}

select = int(input("""지금 기분이 어떠신가요?
1. 잘 모르겠다.
2. 행복하다.
3. 슬프다.
4. 답답하다.
5. 따분하다.
"""))

if select == 1: # 잘 모르겠다.
  age = int(input("나이를 입력하시오."))

  if age > 40:
    ans = input("배고프신가요? ( yes or no ) ")
    if ans == 'yes':
      msg = music_list_yes[40][select-1]
      print(msg)
    elif ans == 'no':
      msg = music_list_no[40][select-1]
      print(msg)
    else:
      print("잘못입력하셨습니다.")

  elif age > 30:
    ans = input("배고프신가요? ( yes or no ) ")
    if ans == 'yes':
      msg = music_list_yes[30][select-1]
      print(msg)
    elif ans == 'no':
      msg = music_list_no[30][select-1]
      print(msg)
    else:
      print("잘못입력하셨습니다.")

  elif age > 20:
    ans = input("배고프신가요? ( yes or no ) ")
    if ans == 'yes':
      msg = music_list_yes[20][select-1]
      print(msg)
    elif ans == 'no':
      msg = music_list_no[20][select-1]
      print(msg)
    else:
      print("잘못입력하셨습니다.")

  elif age > 10:
    print("양심이 없으시군요.")
    msg = music_list_yes[10][select-1]
    print(msg)

  else:
    print("나이를 잘못 입력하셨습니다.")


if select == 2: # 행복하다
  age = int(input("나이를 입력하시오."))

  if age > 40:
    ans = input("안녕하신가요? ( yes or no ) ")
    if ans == 'yes':
      msg = music_list_yes[40][select-1]
      print(msg)
    elif ans == 'no':
      msg = music_list_no[40][select-1]
      print(msg)
    else:
      print("잘못입력하셨습니다.")

  elif age > 30:
    ans = input("안녕하신가요? ( yes or no ) ")
    if ans == 'yes':
      msg = music_list_yes[30][select-1]
      print(msg)
    elif ans == 'no':
      msg = music_list_no[30][select-1]
      print(msg)
    else:
      print("잘못입력하셨습니다.")

  elif age > 20:
    ans = input("안녕하신가요? ( yes or no ) ")
    if ans == 'yes':
      msg = music_list_yes[20][select-1]
      print(msg)
    elif ans == 'no':
      msg = music_list_no[20][select-1]
      print(msg)
    else:
      print("잘못입력하셨습니다.")

  elif age > 10:
    print("양심이 없으시군요.")
    msg = music_list_yes[10][select-1]
    print(msg)

  else:
    print("나이를 잘못 입력하셨습니다.")


if select == 3: # 슬프다.
  age = int(input("나이를 입력하시오."))

  if age > 40:
    ans = input("F신가요? ( yes or no ) ")
    if ans == 'yes':
      msg = music_list_yes[40][select-1]
      print(msg)
    elif ans == 'no':
      msg = music_list_no[40][select-1]
      print(msg)
    else:
      print("잘못입력하셨습니다.")

  elif age > 30:
    ans = input("F신가요? ( yes or no ) ")
    if ans == 'yes':
      msg = music_list_yes[30][select-1]
      print(msg)
    elif ans == 'no':
      msg = music_list_no[30][select-1]
      print(msg)
    else:
      print("잘못입력하셨습니다.")

  elif age > 20:
    ans = input("F신가요? ( yes or no ) ")
    if ans == 'yes':
      msg = music_list_yes[20][select-1]
      print(msg)
    elif ans == 'no':
      msg = music_list_no[20][select-1]
      print(msg)
    else:
      print("잘못입력하셨습니다.")

  elif age > 10:
    print("양심이 없으시군요.")
    msg = music_list_yes[10][select-1]
    print(msg)

  else:
    print("나이를 잘못 입력하셨습니다.")


if select == 4: # 답답하다.
  age = int(input("나이를 입력하시오."))

  if age > 40:
    ans = input("솔로신가요? ( yes or no ) ")
    if ans == 'yes':
      msg = music_list_yes[40][select-1]
      print(msg)
    elif ans == 'no':
      msg = music_list_no[40][select-1]
      print(msg)
    else:
      print("잘못입력하셨습니다.")

  elif age > 30:
    ans = input("솔로신가요? ( yes or no ) ")
    if ans == 'yes':
      msg = music_list_yes[30][select-1]
      print(msg)
    elif ans == 'no':
      msg = music_list_no[30][select-1]
      print(msg)
    else:
      print("잘못입력하셨습니다.")

  elif age > 20:
    ans = input("솔로신가요? ( yes or no ) ")
    if ans == 'yes':
      msg = music_list_yes[20][select-1]
      print(msg)
    elif ans == 'no':
      msg = music_list_no[20][select-1]
      print(msg)
    else:
      print("잘못입력하셨습니다.")

  elif age > 10:
    print("양심이 없으시군요.")
    msg = music_list_yes[10][select-1]
    print(msg)

  else:
    print("나이를 잘못 입력하셨습니다.")

  

if select == 5: # 따분하다.
  age = int(input("나이를 입력하시오."))

  if age > 40:
    ans = input("건강하신가요? ( yes or no ) ")
    if ans == 'yes':
      msg = music_list_yes[age][select-1]
      print(msg)
    elif ans == 'no':
      msg = music_list_no[age][select-1]
      print(msg)

  elif age > 30:
    ans = input("건강하신가요? ( yes or no ) ")
    if ans == 'yees':
      msg = music_list_yes[age][select-1]
      print(msg)
    elif ans == 'no':
      msg = music_list_no[age][select-1]
      print(msg)

  elif age > 20:
    ans = input("건강하신가요? ( yes or no ) ")
    if ans == 'yes':
      msg = music_list_yes[age][select-1]
      print(msg)
    elif ans == 'no':
      msg = music_list_no[age][select-1]
      print(msg)

  elif age > 10:
    print("양심이 없으시군요.")
    msg = music_list_yes[age][select-1]
    print(msg)

  else:
    print("잘못 입력하셨습니다.")

실행 결과

 

 

'파이썬 > 기본' 카테고리의 다른 글

[파이썬] 컬렉션과 반복  (0) 2023.09.11
[파이썬] 반복문  (0) 2023.09.11
[파이썬] 조건문  (0) 2023.09.06
[파이썬] 연산자  (0) 2023.09.05
[파이썬] 컬렉션 타입, 딕셔너리  (0) 2023.09.05

댓글