1

In the following code:

grades = {
    ("DD1", 1) : 45,
    ("DD1", 2) : 75,
    ("DD1", 3) : 25,
    ("DD1", 4) : 65,

    ("DD2", 1) : 85,
    ("DD2", 2) : 40,
    ("DD2", 3) : 70,
    ("DD2", 4) : 80,
    }
def listGrades(dataBase, group):
    list_of_score = []
    for key, value in dataBase.items():
            if key[0] == group:
                list_of_score.append(value)
    return list_of_score

dataBase = input("Which database?")
group = input("which group?")

print(listGrades(dataBase, group))

Upon debugging, I get this:

Which database?grades
which group?DD1
Traceback (most recent call last):
  File "C:\Users\DuckyGoh\Desktop\1003\tt3.py", line 51, in <module>
    print(listGrades(dataBase, group))
  File "C:\Users\DuckyGoh\Desktop\1003\tt3.py", line 43, in listGrades
    for key, value in dataBase.items():
AttributeError: 'str' object has no attribute 'items'

Can someone educate me on my mistake and how to resolve this.

5
  • 1
    Did you enter 'grades' when asked by input and hope that it would be replaced by the content of the grades dict? Commented Oct 12, 2019 at 16:03
  • I would like to make use of an input to allow the user to select which dict he/she would like to retrieve values from(grades, grades...), is there a way to do so? @Norrius Commented Oct 12, 2019 at 16:04
  • I would like to have multiple dictionaries(currently only have one dict grades), eventually the program will have, perhaps, grades1, grades2, grades3. Upon input from the user, it would return values from the respective dict. @ThierryLathuille Commented Oct 12, 2019 at 16:07
  • 1
    are u using python3? Commented Oct 12, 2019 at 16:08
  • yes, 3.7.4 to be exact. @arunp9294 Commented Oct 12, 2019 at 16:09

1 Answer 1

3
if sys.version_info >= (3,):
    def input(__prompt: Any = ...) -> str: ...
else:
    def input(__prompt: Any = ...) -> Any: ...
    def intern(__string: str) -> str: ...

From python 3 onwards input is accepted as string. For python2, the above could accept dataBase as object available in globals.

grades = {
    ("DD1", 1) : 45,
    ("DD1", 2) : 75,
    ("DD1", 3) : 25,
    ("DD1", 4) : 65,

    ("DD2", 1) : 85,
    ("DD2", 2) : 40,
    ("DD2", 3) : 70,
    ("DD2", 4) : 80,
}

dataBases = {
    'grades': grades
}

def listGrades(dataBase, group):
    list_of_score = []
    for key, value in dataBase.items():
        if key[0] == group:
            list_of_score.append(value)
    return list_of_score

dataBase = input("Which database?")
group = input("which group?")

dataBase = dataBases.get(dataBase, {})

print(listGrades(dataBase, group))
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your input, @arunp9294 that's very interesting to know. lets say I have another dict "grades2", it seems like I have to add 'grades2': grades2 to dataBases dict. Is this the only way allow user to select dict?
You can also use dataBase = globals().get(dataBase, {}) but i wouldn't recomment it
I see, thanks for your input @arunp9294. I will look into your solution myself! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.