-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguestbook.py
67 lines (49 loc) · 1.9 KB
/
guestbook.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
# A guestbook program
# Defining the functions first, before asking the user if they want to read/write
import json
import datetime
def guestbook(action):
# If the user chooses to read the guestbook:
if action == "r":
file_handle = open("guestbook.json", "r", encoding="utf-8")
content = file_handle.read()
file_handle.close()
entries = json.loads(content)
# Checking first if the guestbook is empty.
# If not, the messages are printed. If it's empty,
# the user is informed.
for entry in entries:
result = bool(entry)
if result:
message = entry["message"]
time = entry["time"]
print(f"{time}: {message}")
else:
print("No messages.")
# If the user has chosen to write in the guestbook:
else:
file_handle = open("guestbook.json", "r", encoding="utf-8")
content = file_handle.read()
file_handle.close()
entries = json.loads(content)
# Requesting to enter a new message
entry_message = input("Write a new message: \n")
# Adding a timestamp:
entry_time = datetime.datetime.now().strftime("%d.%m.%Y %H:%M")
# Saving the message as a new entry in a dictionary:
new_entry = {
"message": entry_message,
"time": entry_time
}
# Adding the new entry to entries:
entries.append(new_entry)
# Converting entries to json:
json_data = json.dumps(entries, indent=2)
# Adding that to guestbook.json:
file = open("guestbook.json", "w")
file.write(json_data)
file.close()
# Informing the user that the message has been saved:
print("Viesti tallennettu vieraskirjaan.")
action = input("Would you like to read the guestbook or write in it? (r/w)? \n")
guestbook(action)