Syntax error in dictionary with multiple values in a single key

Question:

I am learning about dictionaries in Python and creating multiple values inside a single key. I have followed his instructions (from 2016) to a T, and even compared our code 1 to 1 and it is exact – yet mine yields a syntax error when trying to run the script. I am unsure if I am truly missing something or if there was just an update to Python from 2016 to now that renders the way he is teaching this example less useful now?

In the hypothetical I am being taught we are adding students to a dictionary with the key being their names and the values being a list with their student ID, age, and grade.

When I go to run this script in IDLE it tells me I have an invalid syntax and highlighted the bolded string quotation – "

This is my script with the bolded quotation that yields a syntax error:

students = {
            "Alice":["ID0001", 26, "A"],
            "Bob":["ID0002", 27, "B"],
            "Claire":[**"**ID0003" 17,"C"],
            "Dan":["ID0004", 21, "D"],
            "Emma":["ID0005", 22, "E"]
            }
print(students["Dan"][1])

I would love to say I have tried other ways to fix it but sadly I am just learning about dictionaries, that is how early in my Python journey I am. So not only do I not know where to start with troubleshooting, but I have followed my instructors example to a T so I am utterly lost and unable to continue with the lecture and demonstrations at a hands-on level.

EDIT :
The asterisks in the script itself were not in my original script, it was simply me trying to bold the quotation that gave me the syntax error and that made it automatically encased in 2 asterisks so I assumed seasoned python users knew it might mean it is bolding or something silly like that – again very new…

Asked By: Journee

||

Answers:

You were missing a comma in Dan’s data array.
Also you should not prefix strings with **
Try this:
Also for python related things you can refer any python related course on youtube or official python documentation.

students = {
            "Alice":["ID0001", 26, "A"],
            "Bob":["ID0002", 27, "B"],
            "Claire":["**ID0003", 17,"C"],
            "Dan":["ID0004", 21, "D"],
            "Emma":["ID0005", 22, "E"]
            }
print(students["Dan"][1])
Answered By: Gunesh Shanbhag

I simply removed the ** outside of the brackets in the third entry and added a comma prior to the 17 in the third entry as well to to have an entry that looks like this:

students = {
            "Alice":["ID0001", 26, "A"],
            "Bob":["ID0002", 27, "B"],
            "Claire":["**ID0003", 17,"C"],
            "Dan":["ID0004", 21, "D"],
            "Emma":["ID0005", 22, "E"]
            }
print(students["Dan"][1])

This cleared up both syntax errors and as well allowed to print the second entry for "Dan" which is "21" due to the indexing always starting with [0].

Answered By: Luki