Why does this work?


#1



HI all,

I'm a bit confused at the code that works. Why are brackets not needed after "Lloyd"? I originally had it like this.

lloyd = {
    "name": "Lloyd" [],
    "homework": [],
    "quizzes": [],
    "tests": [],
    }
alice = {
    "name": "Alice" [],
    "homework": [],
    "quizzes": [],
    "tests": [],
    }
tyler = {
    "name": "Tyler" [],
    "homework": [],
    "quizzes": [],
    "tests": [],
    }


But then only when I deleted the brackets for the names , only then did it work.


lloyd = {
    "name": "Lloyd",
    "homework": [],
    "quizzes": [],
    "tests": [],
    }
alice = {
    "name": "Alice",
    "homework": [],
    "quizzes": [],
    "tests": [],
    }
tyler = {
    "name": "Tyler",
    "homework": [],
    "quizzes": [],
    "tests": [],
    }

I don't understand why this is to be the case; we still need a list regardless of there being a key or not, so doesn't that mean that brackets are still necessary? Any advice would be greatly appreciated.


#2

The name property has a string value so we can acces the name directly. We don't need a data structure since the string is the only data associated with the name key.

    print lloyd.name    # Lloyd
    print alice.name    # Alice
    print tyler.name    # Tyler

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.