try:
    # 파일 초기화 및 쓰기 모드로 열기
    file = open("C:/tmp/2024100.txt", "w", encoding="utf-8")
    
    # 초기 코크(콜라) 개수 입력받기
    coke_count = int(input("콜라 개수를 입력하세요: "))
    print("콜라 개수: %d개" % coke_count)
    file.write(f"초기 콜라 개수: {coke_count}개\\n")
    
    while coke_count > 0:
        # 돈 입력받기
        money = int(input("돈을 넣어 주세요: "))
        file.write(f"{money}원을 받았습니다.\\n")
        
        if money > 1000:
            change = money - 1000
            print(f"거스름돈 {change}원을 반환합니다.")
            file.write(f"거스름돈: {change}원 반환\\n")
            coke_count -= 1
        elif money == 1000:
            print("콜라를 드립니다.")
            file.write("콜라를 드렸습니다.\\n")
            coke_count -= 1
        else:
            print("돈이 부족합니다. 반환합니다.")
            file.write("돈이 부족하여 반환하였습니다.\\n")
            continue
        
        # 남은 콜라 개수 기록
        file.write(f"남은 콜라 개수: {coke_count}개\\n")
    
    print("콜라가 다 떨어졌습니다.")
    file.write("콜라가 모두 소진되었습니다.\\n")

except Exception as e:
    # 오류 발생 시 메시지 출력
    print(f"오류가 발생했습니다: {e}")
finally:
    # 파일 닫기
        file.close()
        print("파일이 정상적으로 닫혔습니다.")

import json

try:
    # 파일 초기화 및 쓰기 모드로 열기
    file = open("C:/tmp/2024100.txt", "w", encoding="utf-8")
    json_file_path = "C:/tmp/2024100.json"  # JSON 파일 경로
    
    # 초기 콜라 개수 입력받기
    coke_count = int(input("콜라 개수를 입력하세요: "))
    print("콜라 개수: %d개" % coke_count)
    file.write(f"초기 콜라 개수: {coke_count}개\\n")
    
    # JSON 데이터를 저장할 리스트
    sales_data = [{"초기 콜라 개수": coke_count}]
    
    while coke_count > 0:
        # 돈 입력받기
        money = int(input("돈을 넣어 주세요: "))
        file.write(f"{money}원을 받았습니다.\\n")
        
        # JSON 기록용 딕셔너리
        transaction = {"투입 금액": money}
        
        if money > 1000:
            change = money - 1000
            print(f"거스름돈 {change}원을 반환합니다.")
            file.write(f"거스름돈: {change}원 반환\\n")
            coke_count -= 1
            transaction["거스름돈"] = change
            transaction["상태"] = "성공"
        elif money == 1000:
            print("콜라를 드립니다.")
            file.write("콜라를 드렸습니다.\\n")
            coke_count -= 1
            transaction["상태"] = "성공"
        else:
            print("돈이 부족합니다. 반환합니다.")
            file.write("돈이 부족하여 반환하였습니다.\\n")
            transaction["상태"] = "실패"
            transaction["사유"] = "금액 부족"
        
        # 남은 콜라 개수 기록
        file.write(f"남은 콜라 개수: {coke_count}개\\n")
        transaction["남은 콜라 개수"] = coke_count
        sales_data.append(transaction)
    
    print("콜라가 다 떨어졌습니다.")
    file.write("콜라가 모두 소진되었습니다.\\n")
    sales_data.append({"상태": "판매 종료", "남은 콜라 개수": 0})
    
    # JSON 파일 저장
    with open(json_file_path, "w", encoding="utf-8") as json_file:
        json.dump(sales_data, json_file, ensure_ascii=False, indent=4)

except Exception as e:
    # 오류 발생 시 메시지 출력
    print(f"오류가 발생했습니다: {e}")
finally:
    # 파일 닫기
    if 'file' in locals() and not file.closed:
        file.close()
        print("파일이 정상적으로 닫혔습니다.")
import json

marks = [90, 25, 67, 45, 80]  # 학생들의 시험 점수 리스트
results = []  # JSON에 저장될 결과 리스트
number = 0    # 학생 번호

try:
    # JSON 파일 열기
    json_file_path = "results.json"
    file = open(json_file_path, "w", encoding="utf-8")

    for mark in marks:
        number += 1
        if mark < 60:
            result = {"학생 번호": number, "결과": "불합격", "점수": mark}
            print(f"{number}번 학생은 불합격입니다.")
        else:
            result = {"학생 번호": number, "결과": "합격", "점수": mark}
            print(f"{number}번 학생은 합격입니다.")

        results.append(result)

    # JSON 파일에 결과 저장 (ensure_ascii=False로 한국어 저장)
    json.dump(results, file, ensure_ascii=False, indent=4)

except Exception as e:
    print(f"오류가 발생했습니다: {e}")
finally:
        file.close()
        print("JSON 파일이 정상적으로 닫혔습니다.")
marks = [90, 25, 67, 45, 80]  # 학생들의 시험 점수 리스트
number = 0  # 학생 번호

try:
    # 파일 열기 (쓰기 모드, UTF-8 인코딩)
    file_path = "results.txt"
    file = open(file_path, "w", encoding="utf-8")

    for mark in marks:
        number += 1
        if mark < 60:
            result = f"{number}번 학생은 불합격입니다. 점수: {mark}\\n"
        else:
            result = f"{number}번 학생은 합격입니다. 점수: {mark}\\n"
        
        print(result.strip())  # 화면 출력
        file.write(result)     # 파일에 기록

except Exception as e:
    print(f"오류가 발생했습니다: {e}")
finally:
    if 'file' in locals() and not file.closed:
        file.close()
        print("텍스트 파일이 정상적으로 닫혔습니다.")