Python 데이터 저장하기 - dictionary

eoblue·2022년 7월 28일
0

dictionary를 이용해서 데이터를 2가지 방식으로 저장해보겠습니다.

  1. {"취미는 무엇인가요?" : "테니스입니다"
    , "특기는 무엇입니까" : "농구입니다"}
    이런 식으로 1개의 dictionary에 모든 데이터를 저장할 수도 있고
total_dictionary = {}

while True:
    question = input("질문을 입력해주세요 : ")
    if question == "q":
        break
    else:
        total_dictionary[question] = ""

for i in total_dictionary:
    print(i)
    answer = input("답변을 입력해주세요 : ")
    total_dictionary[i] = answer
print(total_dictionary)

2.[{"질문" : "취미는 무엇입니까?", "답변" : "테니스입니다"},
{"질문" : "특기는 무엇입니까?", "답변" : "농구입니다"}]
이렇게 dictionary를 list로 구성하는 방법도 있다.

total_list = []

while True:
    question = input("질문을 입력해주세요 : ")
    if(question == "q"):
        break
    else:
        total_list.append({"질문" : question, "답변" : ""})

for i in total_list:
    print(i["질문"])
    answer = input("답변을 입력해주세요 : ")
    i["답변"] = answer
print(total_list)
profile
문과생(~2021) -> 컴공(2022~), 멋사 10기 백엔드(2022~)

0개의 댓글