Python Introducing Dictionaries Introducing Dictionaries Update and Mutate Dictionaries

Luke Tate
PLUS
Luke Tate
Courses Plus Student 2,244 Points

[SOLVED] I can't seem to change any value of the dictionary course, without getting a syntax error.

The error is as stated below:

File "dictionaries.py", line 3                                                                                                                                                                                                                                                                                    
    print(course['teacher']='Luke')                                                                                                                                                                                                                                                                                 
         ^                                                                                                                                                                                                                                                                                                          
SyntaxError: keyword can't be an expression  

This is the code I adjusted:

course = {'teacher':'Ashley', 'title':'Introducing Dictionaries', 'level':'Immediate'}

print(course['teacher']='Luke')
Luke Tate
Luke Tate
Courses Plus Student 2,244 Points

I do apologize! I fixed an easy issue, with just printing out the variable itself, not the mutated variable.

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Luke Tate! The problem here is that you've got a reassignment happening inside the print() statement. But the print() function can't take an assignment without error. However, if you split that up just a bit, you get back the results you're expecting:

course['teacher']='Luke'
print(course['teacher'])
# prints out "Luke"

Hope this helps! :sparkles:

Luke Tate
Luke Tate
Courses Plus Student 2,244 Points

Thanks for your advice!!!! However, it exclusively prints out that specific key value, ignore everything else, including the dictionary it was in.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Luke Tate you can also just print out the entire course if you like :smiley:

course = {'teacher':'Ashley', 'title':'Introducing Dictionaries', 'level':'Immediate'}

course['teacher']='Luke'
print(course)
# prints out {'teacher': 'Luke', 'title': 'Introducing Dictionaries', 'level': 'Immediate'}

The problem that you originally had is that you had an assignment inside a print statement.

This would also not work for that same reason :smiley:

x = 100
print(x = 200)