Последняя активность 1731350954

Версия cb55dd9a708a79ccc53c07a980492517e6b489b8

TastyHint2.md Исходник

If we have thought about this:


tasks = {}   # creates an empty dictionary

tasks['Buy Milk'] = 'not yet'
tasks['Start Lab'] = 'completed'

tasks['Finish Lab'] = 'not yet'

# to test if something is done?

task_name = 'Buy Milk'

if tasks[task_name] == 'not yet':
  # task is unfinished 
  pass
else:
  # task is 'complete'
  pass


# to list all the tasks

for task_name, status in enumerate(tasks.items()):
  print("- ", task_name, status) 

We need to think about how to put thse ideas into the class. We might...

class Tasty:
  
  def __init__(self):
    self.tasks = {}

  def add_task(self, task_name):
    """
    Add a new task to the user tasks.
    """
    if task_name not in self.tasks:
        self.tasks[task_name] = "not yet"
    else:
        print("Task already added.")


  def display_tasks(self):
    for task_name, status in enumerate(self.tasks.items()):
      print("- ", task_name, status)

See how those things went from just trial code to methods in the class?

Yes, and... the self variable is how you know that thing is a method and not just a function.