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 5e95c52

Browse files
committed
added most to all of UI and SQL commands, user input errors
1 parent 3d558c2 commit 5e95c52

File tree

3 files changed

+99
-60
lines changed

3 files changed

+99
-60
lines changed

‎.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
venv
2+
checklist.txt

‎planner.db

-8 KB
Binary file not shown.

‎planner.py

Lines changed: 98 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,136 @@
11
import sqlite3 as sql
2+
import time
23

34
connect = sql.connect('planner.db')
45
cursor = connect.cursor()
56

6-
cursor.execute("CREATE TABLE IF NOT EXISTS tasks (type TEXT, task TEXT, time REAL)")
7+
cursor.execute("CREATE TABLE IF NOT EXISTS tasks (task_id INTEGER PRIMARY KEY, task TEXT, type TEXT, time REAL)")
8+
9+
def get_choice(max, phrase, do_phrase=True):
10+
choice = 0
11+
while choice > max or choice < 1:
12+
try:
13+
if do_phrase:
14+
print(phrase)
15+
choice = int(input('-> '))
16+
if choice > max or choice < 1:
17+
print('\nNot a valid number.')
18+
time.sleep(.5)
19+
if choice <= max and choice > 0:
20+
return choice
21+
except ValueError:
22+
print('\nNot a valid number.')
23+
time.sleep(.5)
24+
25+
def get_tasks():
26+
cursor.execute("SELECT * FROM tasks")
27+
return cursor.fetchall()
28+
29+
def display_tasks(tasks, numbers=True):
30+
if numbers:
31+
print('\n{:<5} {:<15} {:<15} {:<15}'.format('ID', 'Task', 'Type', 'Time'))
32+
print('{:<5} {:<15} {:<15} {:<15}'.format('---', '-----', '-----', '-----'))
33+
for task in tasks:
34+
print('{:<5} {:<15} {:<15} {:<15}'.format(task[0] + 1, task[1], task[2], task[3]))
35+
else:
36+
print('\n{:<15} {:<15} {:<15}'.format('Task', 'Type', 'Time'))
37+
print('{:<15} {:<15} {:<15}'.format('-----', '-----', '-----'))
38+
for task in tasks:
39+
print('{:<15} {:<15} {:<15}'.format(task[1], task[2], task[3]))
740

841
print('Welcome to your planner!')
942

1043
choice = None
1144
while choice != 3:
12-
print('\nWhat would you like to do?')
13-
print('1). View Tasks')
14-
print('2). Edit Planner')
15-
print('3). Quit')
16-
try:
17-
choice = int(input('-> '))
18-
except ValueError:
19-
print('\nNot a valid number.')
45+
# Get the choice number to know what to do: view the tasks, edit the tasks, or end the program
46+
choice = get_choice(3, '\nWhat would you like to do?\n1). View Tasks\n2). Edit Planner\n3). Quit')
2047

2148
if choice == 1:
22-
cursor.execute("SELECT * FROM tasks")
23-
print('\n{:<15} {:<15} {:<15}'.format('Type', 'Task', 'Time'))
24-
25-
for task in cursor.fetchall():
26-
print('{:<15} {:<15} {:<15}\n'.format(task[0], task[1], task[2]))
49+
#if user chooses choice 1, display all tasks, the task type, and the task time
50+
tasks = get_tasks()
51+
display_tasks(tasks, False)
2752

2853
if choice == 2:
29-
print('\nWould you like to:')
30-
print('1). Add')
31-
print('2). Edit')
32-
print('3). Delete')
33-
print('4). Reset')
34-
print('5). Go back')
35-
try:
36-
choice = int(input('-> '))
37-
except ValueError:
38-
print('\nNot a valid number.')
39-
54+
# if user choose choice 2, display the choices for editing and allow for answer
55+
choice = get_choice(5, '\nWould you like to:\n1). Add\n2). Edit\n3). Delete\n4). Reset planner\n5). Go back')
4056
if choice == 1:
57+
# 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
4158
task = input('\nTask: ')
4259
type = input('Type of task: ')
43-
time = float(input('Time to complete in hours: '))
44-
values = (type, task, time)
45-
cursor.execute("INSERT INTO tasks VALUES (?, ?, ?)", values)
60+
hours = -1
61+
while hours < 0:
62+
try:
63+
hours = float(input('Time to complete in hours: '))
64+
except ValueError:
65+
print('\nNot a valid number.\n')
66+
time.sleep(.5)
67+
tasks = get_tasks()
68+
values = (len(tasks), task, type, hours)
69+
cursor.execute("INSERT INTO tasks VALUES (?, ?, ?, ?)", values) #insert the ID, and the inputs from the user to the database
4670
connect.commit()
47-
4871
elif choice == 2:
49-
cursor.execute("SELECT * FROM tasks")
50-
tasks = cursor.fetchall()
51-
print('\nWhich number task would you like to edit? ')
52-
for i, task in enumerate(tasks):
53-
print(f'{i + 1}. {task[1]}')
54-
choice = int(input('-> '))
55-
task_name =tasks[choice - 1][1]
56-
print('\nWould you like to edit:')
57-
print('1). Task')
58-
print('2). Type')
59-
print('3). Time')
60-
try:
61-
choice = int(input('-> '))
62-
except ValueError:
63-
print('\nNot a valid number.')
72+
tasks = get_tasks()
73+
display_tasks(tasks)
74+
choice = get_choice(len(tasks), '\nWhich number task would you like to edit? ')
75+
task_id = choice - 1
76+
choice = get_choice(3, '\nWould you like to edit:\n1). Task\n2). Type\n3). Time')
77+
6478
if choice == 1:
6579
task = input('\nTask: ')
66-
values = (task, task_name)
67-
cursor.execute("UPDATE tasks SET task = ? WHERE task = ?", values)
80+
values = (task, task_id)
81+
cursor.execute("UPDATE tasks SET task = ? WHERE task_id = ?", values)
6882
connect.commit()
6983
elif choice == 2:
70-
type = input('Type of task: ')
71-
values = (type, task_name)
72-
cursor.execute("UPDATE tasks SET type = ? WHERE task = ?", values)
84+
type = input('\nType of task: ')
85+
values = (type, task_id)
86+
cursor.execute("UPDATE tasks SET type = ? WHERE task_id = ?", values)
7387
connect.commit()
7488
elif choice == 3:
7589
choice = None
76-
time = float(input('Time to complete in hours: '))
77-
values = (time, task_name)
78-
cursor.execute("UPDATE tasks SET time = ? WHERE task = ?", values)
90+
hours = -1
91+
while hours < 0:
92+
try:
93+
hours = float(input('\nTime to complete in hours: '))
94+
if hours < 0:
95+
print('\nNot a valid number.')
96+
time.sleep(.5)
97+
except ValueError:
98+
print('\nNot a valid number.')
99+
time.sleep(.5)
100+
hours = -1
101+
values = (hours, task_id)
102+
cursor.execute("UPDATE tasks SET time = ? WHERE task_id = ?", values)
79103
connect.commit()
80-
else:
81-
print('Not a valid number.')
82-
83-
84-
104+
85105

86106

87107
elif choice == 3:
88-
delete = int(input('\nWhich number item would you like to delete? '))
108+
tasks = get_tasks()
109+
choice = 0
110+
while choice < 1 or choice > len(tasks):
111+
display_tasks(tasks)
112+
choice = get_choice(len(tasks), '\nWhich number task would you like to delete?')
113+
task_id = choice - 1
114+
values = (task_id,)
115+
cursor.execute("DELETE FROM tasks WHERE task_id = ?", values)
116+
connect.commit()
117+
tasks = get_tasks()
118+
for task in tasks:
119+
if task[0] > task_id:
120+
values = (task[0] - 1, task_id)
121+
cursor.execute("UPDATE tasks SET task_id = ? WHERE task_id = ?", values)
122+
connect.commit()
89123
choice = None
124+
125+
126+
127+
90128
elif choice == 4:
91-
verify = input('\nAre you sure you want to reset the planner? (y/n) ')
129+
verify = input('\nAre you sure you want to reset the planner (y/n)? ')
92130
elif choice == 5:
93131
pass
94132
else:
95-
print('Not a valid number.')
133+
pass
96134

97135

98136

0 commit comments

Comments
(0)

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