-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.py
68 lines (61 loc) · 2.49 KB
/
create.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
import sqlite3
import re
from datetime import datetime
from fmt import fmt
conn = sqlite3.connect("userdecks.db")
cursor = conn.cursor()
def create(username):
try:
print("\nDeck name must be 3-64 characters in length, with English characters, numbers, spaces, underscore, hyphen, period allowed.")
name = input("Enter deck name: ").strip()
valid = all(
x.isalnum() or x in " _-." for x in name) and len(name) > 3 and len(name) <= 64
cursor.execute(
f"SELECT * FROM userdecks WHERE deck = '{name}'")
unique = cursor.fetchone() == None
note = input("Enter description/notes: ").strip()
if valid and unique:
print("\nType QUIT to stop.\n")
filename = fmt(name)
t = datetime.now()
m = str(t.minute)
if len(m) == 1:
m = "0" + m
s = str(t.second)
if len(s) == 1:
s = "0" + s
f = open(filename, "w")
f.write(f"""Name: {name}
Note: {note}
Author: {username}
Created On: {t.day}/{t.month}/{t.year} {t.hour}:{m}.{s}
\n""") # The unindent prevents unnecessary indents in the .deck file.
cursor.execute(
f"INSERT INTO userdecks VALUES ('{username}', '{name}')")
conn.commit()
questions = []
while True:
question = input("Enter question: ").strip()
answer = input("Enter answer: ").strip()
if question == "QUIT":
break
else:
with open(filename, "a") as f:
if "" in [question, answer]:
print("There cannot be empty questions/answers!")
elif question in questions:
print("There cannot be duplicate questions!")
elif ";" in [question, answer]:
print(
"Sorry, but due to a limitation, we cannot have the ';' character in questions/answers.")
else:
f.write(f"{question};{answer}\n")
questions.append(question)
f.close()
elif not unique:
print("This deck already exists")
else:
print("Invalid Name!")
conn.close()
except KeyboardInterrupt:
return