kristofer revised this gist . Go to revision
1 file changed, 1 insertion, 1 deletion
TastyHint2.md
| @@ -26,7 +26,7 @@ for task_name, status in enumerate(tasks.items()): | |||
| 26 | 26 | print("- ", task_name, status) | |
| 27 | 27 | ``` | |
| 28 | 28 | ||
| 29 | - | We need to thing about how to put _into the class_. | |
| 29 | + | We need to think about how to put _into the class_. | |
| 30 | 30 | We might... | |
| 31 | 31 | ||
| 32 | 32 | ```python | |
kristofer revised this gist . Go to revision
1 file changed, 61 insertions
TastyHint2.md(file created)
| @@ -0,0 +1,61 @@ | |||
| 1 | + | ||
| 2 | + | ||
| 3 | + | If we have thought about this: | |
| 4 | + | ||
| 5 | + | ```python | |
| 6 | + | ||
| 7 | + | tasks = {} # creates an empty dictionary | |
| 8 | + | ||
| 9 | + | tasks['Buy Milk'] = 'not yet' | |
| 10 | + | tasks['Start Lab'] = 'completed' | |
| 11 | + | ||
| 12 | + | tasks['Finish Lab'] = 'not yet' | |
| 13 | + | ||
| 14 | + | # to test if something is done? | |
| 15 | + | ||
| 16 | + | task_name = 'Buy Milk' | |
| 17 | + | ||
| 18 | + | if tasks[task_name] == 'not yet': | |
| 19 | + | # task is unfinished | |
| 20 | + | else: | |
| 21 | + | # task is 'complete' | |
| 22 | + | ||
| 23 | + | # to list all the tasks | |
| 24 | + | ||
| 25 | + | for task_name, status in enumerate(tasks.items()): | |
| 26 | + | print("- ", task_name, status) | |
| 27 | + | ``` | |
| 28 | + | ||
| 29 | + | We need to thing about how to put _into the class_. | |
| 30 | + | We might... | |
| 31 | + | ||
| 32 | + | ```python | |
| 33 | + | class Tasty: | |
| 34 | + | ||
| 35 | + | def __init__(self): | |
| 36 | + | self.tasks = {} | |
| 37 | + | ||
| 38 | + | def add_task(self, task_name): | |
| 39 | + | """ | |
| 40 | + | Add a new task to the user tasks. | |
| 41 | + | """ | |
| 42 | + | if task_name not in self.tasks: | |
| 43 | + | self.tasks[task_name] = "not yet" | |
| 44 | + | else: | |
| 45 | + | print("Task already added.") | |
| 46 | + | ||
| 47 | + | ||
| 48 | + | def display_tasks(self): | |
| 49 | + | for task_name, status in enumerate(self.tasks.items()): | |
| 50 | + | print("- ", task_name, status) | |
| 51 | + | ||
| 52 | + | ``` | |
| 53 | + | ||
| 54 | + | See how those things went from just trial code to _methods_ in the class? | |
| 55 | + | ||
| 56 | + | ||
| 57 | + | ||
| 58 | + | ||
| 59 | + | ||
| 60 | + | ||
| 61 | + | ||