-
Notifications
You must be signed in to change notification settings - Fork 0
/
suggestion.py
77 lines (63 loc) · 2.42 KB
/
suggestion.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from cs50 import SQL
import random
# Connect to the database
db = SQL("sqlite:///gifthub.db") # Make sure the database is correctly connected
def suggest(year):
# Get the names and IDs of children who haven't sent letters for the given year
children = db.execute("""
SELECT children.id, children.name
FROM children
LEFT JOIN letters ON children.id = letters.child_id
WHERE letters.year IS NULL OR letters.year <> ?
""", year)
# Debug print statement
print()
print("Children without letters:")
for i, child in enumerate(children):
if i != len(children) - 1:
print(f"{child['name']}", end=", ")
else:
print(f"{child['name']}", end=".")
print()
print()
gift_assignments = []
print("Process:")
for child in children:
child_id = child['id']
child_name = child['name']
last_year = year - 1
# Get the last year's gift category for the child
category_result = db.execute("""
SELECT gifts.category
FROM deliveries
JOIN gifts ON deliveries.gift_id = gifts.id
WHERE deliveries.child_id = ? AND deliveries.year = ?
""", child_id, last_year)
category = category_result[0]['category'] if category_result else None
# Debug print statement
if category:
print(f"{child_name} - Last year's category: {category}", end=" & ")
# Get a random gift from the specified category
gifts = db.execute("""
SELECT model
FROM gifts
WHERE category = ?
AND quantity > 0
""", category)
if gifts:
selected_gift = random.choice(gifts)['model']
print(f"Selected gift: {selected_gift}")
gift_assignments.append((child['name'], selected_gift))
else:
print(f"No available gifts in the category!")
gift_assignments.append((child['name'], "No available gift"))
else:
print(f"{child_name} - No category found for last year")
gift_assignments.append((child['name'], "Unknown desire"))
return gift_assignments
if __name__ == "__main__":
year = int(input("Enter the year: "))
assignments = suggest(year)
print("\nSummery:")
for child, gift in assignments:
print(f"{child}, Gift: {gift}")