Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 380b218

Browse files
committed
all logic added, user error fixes
1 parent 1f92a53 commit 380b218

File tree

1 file changed

+138
-61
lines changed

1 file changed

+138
-61
lines changed

‎planner.py

Lines changed: 138 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,26 @@
44
connect = sql.connect('planner.db')
55
cursor = connect.cursor()
66

7-
cursor.execute("CREATE TABLE IF NOT EXISTS tasks (task_id INTEGER PRIMARY KEY, task TEXT UNIQUE, type TEXT, time REAL)")
7+
cursor.execute("CREATE TABLE IF NOT EXISTS tasks (task_id INTEGER PRIMARY KEY, task TEXT UNIQUE, time REAL, date 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()
827

928
def get_choice(max, phrase, do_phrase=True):
1029
choice = 0
@@ -13,94 +32,159 @@ def get_choice(max, phrase, do_phrase=True):
1332
if do_phrase:
1433
print(phrase)
1534
choice = int(input('-> '))
35+
print()
1636
if choice > max or choice < 1:
17-
print('\nNot a valid number.')
37+
print('Not a valid number.')
1838
time.sleep(.5)
19-
if choice <= max and choice > 0:
20-
return choice
2139
except ValueError:
2240
print('\nNot a valid number.')
2341
time.sleep(.5)
42+
return choice
43+
44+
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")
46+
return cursor.fetchall()
2447

2548
def get_tasks():
26-
cursor.execute("SELECT * FROM tasks")
49+
cursor.execute("SELECT task FROM tasks")
50+
return cursor.fetchall()
51+
52+
def get_types():
53+
cursor.execute("SELECT * FROM types")
2754
return cursor.fetchall()
2855

29-
def display_tasks(tasks):
30-
print('\n{:<15} {:<15} {:<15}'.format('Task', 'Type', 'Time'))
31-
print('{:<15} {:<15} {:<15}'.format('-----', '-----', '-----'))
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+
correct = False
73+
while not correct:
74+
year = input('\nDue date year (yyyy): ')
75+
if len(year) != 4 or int(year) < 0:
76+
print('\nNot a valid number.')
77+
time.sleep(.5)
78+
else:
79+
correct = True
80+
correct = False
81+
while not correct:
82+
caught = False
83+
month = input('\nDue date month (mm): ')
84+
try:
85+
int(month)
86+
except:
87+
caught = True
88+
if caught or len(month) != 2 or int(month) > 12 or int(month) < 1:
89+
print('\nNot a valid number.')
90+
time.sleep(.5)
91+
else:
92+
correct = True
93+
correct = False
94+
while not correct:
95+
day = input('\nDue date day (dd): ')
96+
try:
97+
int(day)
98+
except:
99+
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):
101+
print('\nNot a valid number.')
102+
time.sleep(.5)
103+
else:
104+
correct = True
105+
date = f'{month}-{day}-{year}'
106+
return date
107+
if value < 0:
108+
print('\nNot a valid number.')
109+
time.sleep(.5)
110+
except ValueError:
111+
print('\nNot a valid number.')
112+
time.sleep(.5)
113+
return value
114+
115+
def display_tasks():
116+
tasks = get_all()
117+
print('\n{:<20} {:<20} {:<20} {:<20}'.format('Task', 'Type', 'Due', 'Time'))
118+
print('{:<20} {:<20} {:<20} {:<20}'.format('-----', '-----', '----', '-----'))
32119
for task in tasks:
33-
print('{:<15} {:<15} {:<15}'.format(task[1], task[2], task[3]))
120+
print('{:<20} {:<20} {:<20} {:<1}'.format(task[0], task[1], task[2], task[3], 'hours'))
121+
122+
def display_types():
123+
types = get_types()
124+
print('\n{:<15} {:<15}'.format('Type ID', 'Type'))
125+
print('{:<15} {:<15}'.format('--------', '-----'))
126+
for type in types:
127+
print('{:<15} {:<15}'.format(type[0], type[1]))
34128

35129
print('Welcome to your planner!')
36130

37131
choice = None
38132
while choice != 3:
39-
# Get the choice number to know what to do: view the tasks, edit the tasks, or end the program
40133
choice = get_choice(3, '\nWhat would you like to do?\n1). View Tasks\n2). Edit Planner\n3). Quit')
41134

42135
if choice == 1:
43-
#if user chooses choice 1, display all tasks, the task type, and the task time
44-
tasks = get_tasks()
45-
display_tasks(tasks)
136+
display_tasks()
46137

47-
ifchoice==2:
48-
# if user choose choice 2, display the choices for editing and allow for answer
138+
139+
elifchoice ==2:
49140
choice = get_choice(5, '\nWould you like to:\n1). Add\n2). Edit\n3). Delete\n4). Reset planner\n5). Go back')
141+
50142
if choice == 1:
51-
# if choice is 1 (add a new task) ask for the task name, what type of task it is, and how long it will take
52-
task = input('\nTask: ')
53-
type = input('Type of task: ')
54-
hours = -1
55-
while hours < 0:
56-
try:
57-
hours = float(input('Time to complete in hours: '))
58-
if hours < 0:
59-
print('\nNot a valid number.\n')
60-
time.sleep(.5)
61-
except ValueError:
62-
print('\nNot a valid number.\n')
63-
time.sleep(.5)
64-
tasks = get_tasks()
65-
values = (None, task, type, hours)
66-
cursor.execute("INSERT INTO tasks VALUES (?, ?, ?, ?)", values) #insert the ID, and the inputs from the user to the database
143+
bad = False
144+
passed = False
145+
while not passed:
146+
task = input('Task: ')
147+
tasks = get_tasks()
148+
for i in tasks:
149+
for j in i:
150+
if task == j:
151+
print('\nTask already exists.\n')
152+
time.sleep(.5)
153+
bad = True
154+
if not bad:
155+
passed = True
156+
display_types()
157+
type_id = get_value('type')
158+
hours = get_value('hours')
159+
date = get_value('date')
160+
values = (None, task, hours, date, type_id)
161+
cursor.execute("INSERT INTO tasks VALUES (?, ?, ?, ?, ?)", values)
67162
connect.commit()
163+
68164
elif choice == 2:
69-
tasks = get_tasks()
70-
display_tasks(tasks)
165+
display_tasks()
71166
print('\nWhich task would you like to edit?')
72167
edit = input('-> ')
73168
choice = get_choice(3, '\nWould you like to edit:\n1). Task\n2). Type\n3). Time')
74169
if choice == 1:
75-
task = input('\nTask: ')
170+
task = input('Task: ')
76171
values = (task, edit)
77172
cursor.execute("UPDATE tasks SET task = ? WHERE task = ?", values)
78-
connect.commit()
79173
elif choice == 2:
80-
type=input('\nType of task: ')
81-
values = (type, edit)
82-
cursor.execute("UPDATE tasks SET type = ? WHERE task = ?", values)
83-
connect.commit()
174+
display_types()
175+
type_id = get_value('type', True)
176+
values= (type_id, edit)
177+
cursor.execute("UPDATE tasks SET type_id = ? WHERE task = ?", values)
84178
elif choice == 3:
85179
choice = None
86-
hours = -1
87-
while hours < 0:
88-
try:
89-
hours = float(input('\nTime to complete in hours: '))
90-
if hours < 0:
91-
print('\nNot a valid number.')
92-
time.sleep(.5)
93-
except ValueError:
94-
print('\nNot a valid number.')
95-
time.sleep(.5)
96-
hours = -1
180+
hours = get_value('hours')
97181
values = (hours, edit)
98182
cursor.execute("UPDATE tasks SET time = ? WHERE task = ?", values)
99-
connect.commit()
183+
connect.commit()
184+
100185
elif choice == 3:
101-
tasks = get_tasks()
102186
choice = 0
103-
display_tasks(tasks)
187+
display_tasks()
104188
print('\nWhich task would you like to delete?')
105189
choice = input('-> ')
106190
values = (choice,)
@@ -112,11 +196,4 @@ def display_tasks(tasks):
112196
if verify == 'y':
113197
cursor.execute('DELETE FROM tasks')
114198
else:
115-
pass
116-
elif choice == 5:
117-
pass
118-
119-
120-
121-
122-
199+
pass

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /