How to associate several integers/strings with items

Viewed 69

For a while now I have been trying to create something, which is turning out to be a bit more than I can handle, as I am very new to not only Python but also programming as a whole.

What I want to do is to make an application - a todo-list, basically.

However, every task added by the user must have several integers associated with them (these are also added by the user and they specify some things - e.g. the difficulty of the task among other things).

So firstly, the tasks which the user adds must somehow be stored as a variable (i think?) and integers must be the value of that variable (i think). But how can i write it so the user input(task+integers) are stored in some fashion so that they're associated with each other.

So let's say a user adds a task Task1 and then has to specify some integers 10 and 4attached to that task (difficulty and hours needed for completion - let's say).

The program then needs to add a variable

task1 = 10, 4

The basic idea being that in the end, I want to do some math based on the integers associated with the task. The thing I find difficult to understand is how I make it so the variables and values are created by the application user.

Can somebody point me in some direction or tell me if I am even thinking in the right way?

If I sound like I'm stupid, it is because I kind of am as I am really new to this - sorry - and thanks!

2 Answers

every task added by the user must have several integers associated with them (these are also added by the user and they specify some things - e.g. the difficulty of the task among other things).

This is exactly what classes are for: you can define a new object of type Task which has attributes that you can get and set. Classes are the cornerstone of python development, and something you should be comfortable in using. Virtually everything you do with python involves interacting with objects, whether you realize it or not.

Here's an example of the definition of a task object which has two properties: difficulty and hours. The values default to 1 and 1, but can be specified when creating the object, and later changed after the object is created:

class Task:
    def __init__(self, difficulty=1, hours=1):
        self.difficulty = difficulty
        self.hours = hours

With that, I can create a new task, and print out the attributes of that task:

task1 = Task()
print("difficulty:", task1.difficulty)
print("hours:", task1.hours)

If I want a task of difficulty=10 and hours=4, I can specify that when creating the task:

task2 = Task(difficulty=10, hours=4)
print("difficulty:", task2.difficulty)
print("hours:", task2.hours)

If I want to change the difficulty after the object has been created, I can do so by setting the new value like I would any other variable:

task1.difficulty = 2

Doing math is no different than with any other type of object:

estimate = task1.difficulty * task1.hours

We can even store the estimate back on the task itself:

task2.estimate = task2.difficulty * task2.hours
print(task1.estimate)

Instead of storing the estimate as a value, we can create a function (called a "method" when it's part of a class) that will always compute an up-to-date estimate based on it's own ("self") values:

class Task:
    def __init__(self, difficulty=1, hours=1):
        self.difficulty = difficulty
        self.hours = hours

    def estimate(self):
        return self.difficulty * self.hours

We can then call the function from the object to get an up-to-date estimate:

task3 = Task(hours=3, difficulty=3)
print("estimate of task 3:", task3.estimate())

There's a whole lot more to classes than just that, but hopefully, this gives you enough information to see the value in using classes.

I think the same can be done using python dictionaries, though the tasks will be unordered. Somewhat like so -:

tasklist = {'task1' : [10, 4, 5....], 'task2' : [4, 5, 6]....}

and now incase you want to add a new task -:

tasklist['task3'] = [task_num_1, task_num_2....]

also if you want to change a task's inner number then for some index n at which the number is present in the task number list -:

tasklist['task1'][n] = new_value

Also incase you want to have ordered tasks, then an OrderedDict can be used which is a class from the collections library like so -:

from collections import OrderedDict
tasklist = OrderedDict()
tasklist['task1'] = [1, 2...]
Related