0: try: # 돈 입력받기 money = int(input("돈을 넣어 주세요: ")) transaction = {"투입 금액": money} # JSON 기록용 if money > 1000: change = money - 1000 print(f"거스름돈 {change}원을 반환합니다.") text_file.write(f"{money}원을 받고 거스름돈 {change}원을 반환했습니다.\n") transaction["상태"] = "성공" transact"> 0: try: # 돈 입력받기 money = int(input("돈을 넣어 주세요: ")) transaction = {"투입 금액": money} # JSON 기록용 if money > 1000: change = money - 1000 print(f"거스름돈 {change}원을 반환합니다.") text_file.write(f"{money}원을 받고 거스름돈 {change}원을 반환했습니다.\n") transaction["상태"] = "성공" transact"> 0: try: # 돈 입력받기 money = int(input("돈을 넣어 주세요: ")) transaction = {"투입 금액": money} # JSON 기록용 if money > 1000: change = money - 1000 print(f"거스름돈 {change}원을 반환합니다.") text_file.write(f"{money}원을 받고 거스름돈 {change}원을 반환했습니다.\n") transaction["상태"] = "성공" transact">
import json

try:
    # 파일 초기화 및 쓰기 모드로 열기
    text_file = open("Users/sojeong/2024100.txt", "w", encoding="utf-8")  # 텍스트 파일 생성
    json_file_path = "Users/sojeong/2024100.json"                         # JSON 파일 경로

    # 초기 콜라 개수 입력받기
    coke_count = int(input("콜라 개수를 입력하세요: "))
    print("콜라 개수: %d개" % coke_count)
    text_file.write(f"초기 콜라 개수: {coke_count}개\\n")

    # JSON 파일 초기화
    sales_data = []  # 판매 기록 리스트
    with open(json_file_path, "w", encoding="utf-8") as json_file:
        json.dump({"남은 콜라 개수": coke_count, "판매 기록": sales_data}, json_file)

    # 콜라 판매 반복문
    while coke_count > 0:
        try:
            # 돈 입력받기
            money = int(input("돈을 넣어 주세요: "))
            transaction = {"투입 금액": money}  # JSON 기록용

            if money > 1000:
                change = money - 1000
                print(f"거스름돈 {change}원을 반환합니다.")
                text_file.write(f"{money}원을 받고 거스름돈 {change}원을 반환했습니다.\\n")
                transaction["상태"] = "성공"
                transaction["거스름돈"] = change
                coke_count -= 1
            elif money == 1000:
                print("콜라를 드립니다.")
                text_file.write(f"{money}원을 받고 콜라를 제공했습니다.\\n")
                transaction["상태"] = "성공"
                transaction["메시지"] = "콜라 제공"
                coke_count -= 1
            else:
                print("돈이 부족합니다. 반환합니다.")
                text_file.write(f"{money}원을 받고 반환하였습니다.\\n")
                transaction["상태"] = "실패"
                transaction["메시지"] = "금액 부족"
                # 실패한 거래도 기록
                transaction["남은 콜라 개수"] = coke_count
                sales_data.append(transaction)
                # JSON 파일 저장
                with open(json_file_path, "w", encoding="utf-8") as json_file:
                    json.dump({"남은 콜라 개수": coke_count, "판매 기록": sales_data}, json_file)
                continue  # 다음 입력을 기다림

            # 남은 콜라 개수 기록
            transaction["남은 콜라 개수"] = coke_count
            sales_data.append(transaction)  # JSON 기록 추가
            text_file.write(f"남은 콜라 개수: {coke_count}개\\n")

            # JSON 파일 저장
            with open(json_file_path, "w", encoding="utf-8") as json_file:
                json.dump({"남은 콜라 개수": coke_count, "판매 기록": sales_data}, json_file)
            print("판매 기록이 저장되었습니다.")

        except ValueError:
            print("유효한 숫자를 입력해 주세요.")
            text_file.write("유효하지 않은 입력이 있었습니다.\\n")

    # 콜라가 모두 소진되었을 때
    print("콜라가 다 떨어졌습니다.")
    text_file.write("콜라가 모두 소진되었습니다.\\n")

except Exception as e:
    print(f"프로그램 실행 중 오류가 발생했습니다: {e}")

finally:
    # 텍스트 파일 닫기
    if 'text_file' in locals() and not text_file.closed:
        text_file.close()
        print("텍스트 파일이 정상적으로 닫혔습니다.")

# JSON 파일 최종 확인
try:
    with open(json_file_path, "r", encoding="utf-8") as json_file:
        print("\\n최종 판매 기록:")
        final_data = json.load(json_file)
        print(json.dumps(final_data))
except Exception as e:
    print(f"최종 JSON 파일 읽기 중 오류가 발생했습니다: {e}")