-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject1.py
More file actions
50 lines (40 loc) · 1.57 KB
/
project1.py
File metadata and controls
50 lines (40 loc) · 1.57 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
import pickle
# Function to create a new diary entry
def write_diary_entry():
date_input = input("Enter the date of the diary entry (YYYY-MM-DD): ")
time_input = input("Enter the time of the diary entry (HH:MM:SS): ")
timestamp = f"{date_input}_{time_input}"
entry = input("Write your diary entry:\n")
filename = f"diary_{timestamp}.dat"
with open(filename, 'wb') as file:
pickle.dump(entry, file)
print(f"Diary entry saved to {filename}\n")
# Function to display all diary entries
def display_diary_entries():
files = [file for file in os.listdir('.') if file.startswith("diary_") and file.endswith(".dat")]
if not files:
print("No diary entries found.")
else:
for file in files:
with open(file, 'rb') as f:
entry = pickle.load(f)
timestamp = file.replace("diary_", "").replace(".dat", "")
print(f"--- Entry from {timestamp} ---\n{entry}\n")
# Main program loop
def main():
while True:
print("1. Write a new diary entry")
print("2. Display all diary entries")
print("3. Exit")
choice = input("Enter your choice (1-3): ")
if choice == '1':
write_diary_entry()
elif choice == '2':
display_diary_entries()
elif choice == '3':
print("Exiting program. Goodbye!")
break
else:
print("Invalid choice. Please enter a number from 1 to 3.")
if _name_ == "_main_":
main()