Skip to content

Commit 459d605

Browse files
committed
finished error checking for user input, split date into parts
1 parent 26eda46 commit 459d605

File tree

4 files changed

+271
-4
lines changed

4 files changed

+271
-4
lines changed

planner copy.py

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
import sqlite3 as sql
2+
import time
3+
4+
connect = sql.connect('planner.db')
5+
cursor = connect.cursor()
6+
7+
cursor.execute("CREATE TABLE IF NOT EXISTS tasks (task_id INTEGER PRIMARY KEY, task TEXT UNIQUE, time REAL, year TEXT, month TEXT, day TEXT, type_id INTEGER, FOREIGN KEY(type_id) REFERENCES types(type_id))")
8+
cursor.execute("CREATE TABLE IF NOT EXISTS types (type_id INTEGER PRIMARY KEY, type TEXT UNIQUE)")
9+
10+
cursor.execute('SELECT * FROM types')
11+
if len(cursor.fetchall()) == 0:
12+
count = 0
13+
while count != 5:
14+
count += 1
15+
if count == 1:
16+
values = (None, 'Chores')
17+
elif count == 2:
18+
values = (None, 'Homework')
19+
elif count == 3:
20+
values = (None, 'Work')
21+
elif count == 4:
22+
values = (None, 'Exercise')
23+
elif count == 5:
24+
values = (None, 'Other')
25+
cursor.execute("INSERT INTO types VALUES (?, ?)", values)
26+
connect.commit()
27+
28+
def get_choice(max, phrase, do_phrase=True):
29+
choice = 0
30+
while choice > max or choice < 1:
31+
try:
32+
if do_phrase:
33+
print(phrase)
34+
choice = int(input('-> '))
35+
print()
36+
if choice > max or choice < 1:
37+
print('Not a valid number.')
38+
time.sleep(.5)
39+
except ValueError:
40+
print('\nNot a valid number.')
41+
time.sleep(.5)
42+
return choice
43+
44+
def get_all():
45+
cursor.execute("SELECT ta.task, ty.type, (ta.month || '/' || ta.day || '/' || ta.year) AS date, ta.time FROM tasks ta JOIN types ty ON ta.type_id = ty.type_id ORDER BY ta.year, ta.month, ta.day, ta.time")
46+
return cursor.fetchall()
47+
48+
def get_tasks():
49+
cursor.execute("SELECT task FROM tasks")
50+
return cursor.fetchall()
51+
52+
def get_types():
53+
cursor.execute("SELECT * FROM types")
54+
return cursor.fetchall()
55+
56+
def get_value(data, new=False):
57+
value = -1
58+
while value < 0:
59+
try:
60+
if data == 'hours':
61+
value = float(input('\nTime to complete in hours: '))
62+
elif data == 'type':
63+
while value > 5 or value < 1:
64+
if not new:
65+
value = int(input('\nType ID: '))
66+
else:
67+
value = int(input('\nNew type ID: '))
68+
if value > 5 or value < 1:
69+
print('\nNot a valid number.')
70+
time.sleep(.5)
71+
else:
72+
date = []
73+
correct = False
74+
cursor.execute("SELECT strftime('%Y', date('now'))")
75+
current_date = cursor.fetchall()
76+
for i in current_date:
77+
for j in i:
78+
now_year = j
79+
while not correct:
80+
year = input('\nDue date year (yyyy): ')
81+
if len(year) != 4 or int(year) < 0 or year < now_year:
82+
print('\nNot a valid number.')
83+
time.sleep(.5)
84+
else:
85+
correct = True
86+
date.append(year)
87+
correct = False
88+
while not correct:
89+
caught = False
90+
cursor.execute("SELECT strftime('%m', date('now'))")
91+
current_date = cursor.fetchall()
92+
for i in current_date:
93+
for j in i:
94+
now_month = j
95+
month = input('\nDue date month (mm): ')
96+
try:
97+
int(month)
98+
except:
99+
caught = True
100+
if caught or len(month) != 2 or int(month) > 12 or int(month) < 1 or (month < now_month \
101+
and year == now_year):
102+
print('\nNot a valid number.')
103+
time.sleep(.5)
104+
else:
105+
correct = True
106+
date.append(month)
107+
correct = False
108+
cursor.execute("SELECT strftime('%d', date('now'))")
109+
current_date = cursor.fetchall()
110+
for i in current_date:
111+
for j in i:
112+
now_day = j
113+
while not correct:
114+
day = input('\nDue date day (dd): ')
115+
try:
116+
int(day)
117+
except:
118+
caught = True
119+
if caught or (int(day) < 1) or len(day) != 2 or (int(month) in {1, 3, 5, 7, 8, 10, 12} \
120+
and int(day) > 31) or (int(month) in {4, 6, 9, 11} and int(day) > 30) or (month == \
121+
'02' and (int(year) % 400 == 0 or int(year) % 4 == 0 and int(year) % 100 != 0) \
122+
and int(day) > 29) or (month == '02' and (int(year) % 400 != 0 and int(year) \
123+
% 100 == 0 or int(year) % 4 != 0) and int(day) > 28) or (day < now_day \
124+
and month == now_month and year == now_year):
125+
print('\nNot a valid number.')
126+
time.sleep(.5)
127+
else:
128+
correct = True
129+
date.append(day)
130+
return date
131+
if value < 0:
132+
print('\nNot a valid number.')
133+
time.sleep(.5)
134+
except ValueError:
135+
print('\nNot a valid number.')
136+
time.sleep(.5)
137+
value = -1
138+
return value
139+
140+
def display_tasks():
141+
tasks = get_all()
142+
print('\n{:<20} {:<20} {:<20} {:<20}'.format('Task', 'Type', 'Due', 'Time'))
143+
print('{:<20} {:<20} {:<20} {:<20}'.format('-----', '-----', '----', '-----'))
144+
for task in tasks:
145+
print('{:<20} {:<20} {:<20} {:<1}'.format(task[0], task[1], task[2], task[3], 'hours'))
146+
147+
def display_types():
148+
types = get_types()
149+
print('\n{:<15} {:<15}'.format('Type ID', 'Type'))
150+
print('{:<15} {:<15}'.format('--------', '-----'))
151+
for type in types:
152+
print('{:<15} {:<15}'.format(type[0], type[1]))
153+
154+
print('Welcome to your planner!')
155+
156+
choice = None
157+
while choice != 3:
158+
choice = get_choice(3, '\nWhat would you like to do?\n1). View Tasks\n2). Edit Planner\n3). Quit')
159+
160+
if choice == 1:
161+
display_tasks()
162+
163+
164+
elif choice == 2:
165+
choice = get_choice(5, '\nWould you like to:\n1). Add\n2). Edit\n3). Delete\n4). Reset planner\n5). Go back')
166+
167+
if choice == 1:
168+
passed = False
169+
while not passed:
170+
bad = False
171+
task = input('Task: ')
172+
tasks = get_tasks()
173+
for i in tasks:
174+
for j in i:
175+
if task == j:
176+
print('\nTask already exists.\n')
177+
time.sleep(.5)
178+
bad = True
179+
break
180+
if bad:
181+
break
182+
if not bad:
183+
passed = True
184+
display_types()
185+
type_id = get_value('type')
186+
hours = get_value('hours')
187+
date = get_value('date')
188+
values = (None, task, hours, date[0], date[1], date[2], type_id)
189+
cursor.execute("INSERT INTO tasks VALUES (?, ?, ?, ?, ?, ?, ?)", values)
190+
connect.commit()
191+
192+
elif choice == 2:
193+
display_tasks()
194+
tasks = get_tasks()
195+
bad = True
196+
while bad:
197+
print('\nWhich task would you like to edit?')
198+
edit = input('-> ')
199+
for i in tasks:
200+
for j in i:
201+
if edit == j:
202+
bad = False
203+
break
204+
if not bad:
205+
break
206+
if bad:
207+
print('\nNot a valid task.')
208+
time.sleep(.5)
209+
choice = get_choice(4, '\nWould you like to edit:\n1). Task\n2). Type\n3). Due date\n4). Time')
210+
if choice == 1:
211+
task = input('Task: ')
212+
values = (task, edit)
213+
cursor.execute("UPDATE tasks SET task = ? WHERE task = ?", values)
214+
elif choice == 2:
215+
display_types()
216+
type_id = get_value('type', True)
217+
values = (type_id, edit)
218+
cursor.execute("UPDATE tasks SET type_id = ? WHERE task = ?", values)
219+
elif choice == 3:
220+
choice = None
221+
date = get_value('date')
222+
values = (date[0], date[1], date[2], edit)
223+
cursor.execute("UPDATE tasks SET year = ?, month = ?, day = ? WHERE task = ?", values)
224+
elif choice == 4:
225+
hours = get_value('hours')
226+
values = (hours, edit)
227+
cursor.execute("UPDATE tasks SET time = ? WHERE task = ?", values)
228+
connect.commit()
229+
230+
elif choice == 3:
231+
choice = 0
232+
display_tasks()
233+
print('\nWhich task would you like to delete?')
234+
choice = input('-> ')
235+
values = (choice,)
236+
cursor.execute("DELETE FROM tasks WHERE task = ?", values)
237+
connect.commit()
238+
239+
elif choice == 4:
240+
verify = input('\nAre you sure you want to reset the planner (y/n)? ').lower()
241+
if verify == 'y':
242+
cursor.execute('DELETE FROM tasks')
243+
else:
244+
pass

planner.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def get_choice(max, phrase, do_phrase=True):
4242
return choice
4343

4444
def get_all():
45-
cursor.execute("SELECT ta.task, ty.type, ta.date, ta.time FROM tasks ta JOIN types ty ON ta.type_id = ty.type_id")
45+
cursor.execute("SELECT ta.task, ty.type, ta.date, ta.time FROM tasks ta JOIN types ty ON ta.type_id = ty.type_id ORDER BY ta.date")
4646
return cursor.fetchall()
4747

4848
def get_tasks():
@@ -70,34 +70,55 @@ def get_value(data, new=False):
7070
time.sleep(.5)
7171
else:
7272
correct = False
73+
cursor.execute("SELECT strftime('%Y', date('now'))")
74+
current_date = cursor.fetchall()
75+
for i in current_date:
76+
for j in i:
77+
now_year = j
7378
while not correct:
7479
year = input('\nDue date year (yyyy): ')
75-
if len(year) != 4 or int(year) < 0:
80+
if len(year) != 4 or int(year) < 0 or year < now_year:
7681
print('\nNot a valid number.')
7782
time.sleep(.5)
7883
else:
7984
correct = True
8085
correct = False
8186
while not correct:
8287
caught = False
88+
cursor.execute("SELECT strftime('%m', date('now'))")
89+
current_date = cursor.fetchall()
90+
for i in current_date:
91+
for j in i:
92+
now_month = j
8393
month = input('\nDue date month (mm): ')
8494
try:
8595
int(month)
8696
except:
8797
caught = True
88-
if caught or len(month) != 2 or int(month) > 12 or int(month) < 1:
98+
if caught or len(month) != 2 or int(month) > 12 or int(month) < 1 or (month < now_month \
99+
and year == now_year):
89100
print('\nNot a valid number.')
90101
time.sleep(.5)
91102
else:
92103
correct = True
93104
correct = False
105+
cursor.execute("SELECT strftime('%d', date('now'))")
106+
current_date = cursor.fetchall()
107+
for i in current_date:
108+
for j in i:
109+
now_day = j
94110
while not correct:
95111
day = input('\nDue date day (dd): ')
96112
try:
97113
int(day)
98114
except:
99115
caught = True
100-
if caught or (int(day) < 1) or len(day) != 2 or (int(month) in {1, 3, 5, 7, 8, 10, 12} and int(day) > 31) or (int(month) in {4, 6, 9, 11} and int(day) > 30) or (month == '02' and (int(year) % 400 == 0 or int(year) % 4 == 0 and int(year) % 100 != 0) and int(day) > 29) or (month == '02' and (int(year) % 400 != 0 and int(year) % 100 == 0 or int(year) % 4 != 0) and int(day) > 28):
116+
if caught or (int(day) < 1) or len(day) != 2 or (int(month) in {1, 3, 5, 7, 8, 10, 12} \
117+
and int(day) > 31) or (int(month) in {4, 6, 9, 11} and int(day) > 30) or (month == \
118+
'02' and (int(year) % 400 == 0 or int(year) % 4 == 0 and int(year) % 100 != 0) \
119+
and int(day) > 29) or (month == '02' and (int(year) % 400 != 0 and int(year) \
120+
% 100 == 0 or int(year) % 4 != 0) and int(day) > 28) or (day < now_day \
121+
and month == now_month and year == now_year):
101122
print('\nNot a valid number.')
102123
time.sleep(.5)
103124
else:

test.db

Whitespace-only changes.

test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
year = []
2+
year.append(1, 2, 3)

0 commit comments

Comments
 (0)