-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsum.py
More file actions
61 lines (55 loc) · 2.56 KB
/
sum.py
File metadata and controls
61 lines (55 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def sum_location_expense(trip_name):
"""해당 여행지(trip_name)에서 발생한 공동 지출 금액을 모두 더해서 반환"""
filename = f"{trip_name}_공동.txt"
total = 0
try:
with open(filename, "r", encoding="utf-8") as f:
lines = f.readlines()[1:] # 두 번째 줄부터 읽기
for line in lines:
# 기록 형식: 날짜 | 카테고리 | 금액원
parts = line.strip().split("|")
if len(parts) >= 3:
amount_str = parts[2].strip().replace("원", "").replace(",", "")
try:
amount = int(amount_str)
total += amount
except ValueError:
continue
print(f"📊 총 공동 지출 금액: {total}원")
return total
except FileNotFoundError:
total = 0
return total
def sum_indipay_budget(trip_name, nickname):
"""해당 닉네임(nickname)의 개인 지출 기록을 모두 더해서 반환"""
filename = f"{trip_name}_{nickname}.txt"
total = 0
try:
with open(filename, "r", encoding="utf-8") as f:
lines = f.readlines()[1:] # 두 번째 줄부터 읽기
for line in lines:
parts = line.strip().split("|")
if len(parts) >= 3:
amount_str = parts[2].strip().replace("원", "").replace(",", "")
try:
amount = int(amount_str)
total += amount
except ValueError:
continue
print(f"👤 {nickname}의 총 개인 지출 금액: {total}원")
return total
except FileNotFoundError:
print(f"❌ {nickname}의 지출 기록 파일이 없습니다.")
return 0
def remain_indi_budget(trip_name, nickname, indi_budget):
"""주어진 개인 예산(indi_budget)에서 개인 지출 금액을 빼고 남은 금액 반환"""
spent = sum_indipay_budget(trip_name, nickname)
remain = int(indi_budget) - spent
print(f"💰 {nickname}의 남은 예산: {remain}원\n")
return remain if remain >= 0 else 0
def remain_all_budget(trip_name, all_budget):
"""주어진 공동 예산(all_budget)에서 공동 지출 금액을 빼고 남은 금액 반환"""
spent = sum_location_expense(trip_name)
remain = int(all_budget) - spent
print(f"💼 공동 예산에서 남은 금액: {remain}원\n")
return remain if remain >= 0 else 0