languages = {
'jen': {
'specializes': ['python', 'rust'],
'favorite': 'python',
'programming_experience': '11 years'
},
'sarah': {
'specializes': ['c', 'css', 'cobol'],
'favorite': 'css',
'programming_experience': '4 years',
},
'edward': {
'specializes':['rust, go'],
'favorite': 'go',
'programming_experience': '5 years',
},
'phil': {
'specializes': ['python', 'haskell', 'r'],
'favorite': 'r',
'programming_experience': '19 years',
}
}
for programmer, programmer_info in languages.items():
if programmer_info in languages == ['python']:
print(f"{programmer.title()} will start the web app tonight using Python")
In the last exercise of one of the chapters in the Python Crash Course it said to extend on a program in the chapter. I decided to have dictionaries inside a dictionary. The first thing I thought of to do with it is to select âpythonâ in an if statement and print something different from the rest. I tried stuff like what I posted above, âif programmer_info in languages == [âpythonâ]:â, which doesnât work. Itâs good I tried something with more complexity, but I havenât found an answer to this yet. Any help would be appreciated.
If a programmer, âphilâ for example, âspecializesâ in âpythonâ, I want to print a message to only those programmers.
I tried if programmer_info[specializes] in languages == [âpythonâ]:, but that didnât work. Not that I expected it to because I have no idea what to do really.
Okay. Now you can break the problem down into two steps.
What does Phil specialize in? What code would you use to find that out? What sort of result would you expect to get?
Given that result, how do you know whether Python is one of the specialties? What code would you use to check that result? (Hint: you were on the right track trying to use the word in for the code. Can you think of a way to use the word in when you use English words to describe this step?)
When we answer these questions, we find out:
Once we have Philâs programmer_info, programmer_info['specializes'] tells us what Phil specializes in. We get a list of strings.
We want to know whether Python - i.e., the string 'python' - is in that list of strings. So that is how we write it: 'python' in programmer_info['specializes'].
Iâm not sure what you mean.
If you mean âI thought that I can only put programmer_info['specializes'] on the left-hand side of in, because itâs choosing a keyâ, then I canât understand why you expect such a restriction. Itâs the same thing as how you can write 1 * (2 + 3), and are not forced to write (2 + 3) * 1.
If you mean âI donât think that this code is choosing a keyâ, then I donât understand what you think âchoosing a keyâ means. Where the code says programmer_info['specializes'], 'specializes' is a key, and you have âchosenâ it by typing an apostrophe, the letter s, etc. into your source code.
If you mean âI thought I had to put the thing Iâm looking for on the right-hand side of in, and the thing I want to search on the left-hand sideâ - that also doesnât make any sense, because thatâs also not how it works in English. If I say âis the cat in the house?â, then the cat is what Iâm asking you to look for, and the house is where Iâm asking you to look - not the other way around.
If you mean (and thanks to @MRAB because otherwise I wouldnât have been able to think of this possibility) âwhy am I able to use programmer_info directly like this, and I donât have to choose an entry from languages?â - thatâs what the loop was for. Every time through the loop, programmer will be one of the names ('jen', 'sarah' etc.), and programmer_info will be the corresponding dictionary, which has the 'specializes' entry. Thatâs the purpose of the code for programmer, programmer_info in languages.items():; and if you took that from the previous lesson code and didnât properly understand it, you should go back and review.
If you mean something else, then Iâm sorry but I simply canât understand the thought process.
Thanks for this reply. The solution itself looks logical to me. The reason I didnât know and wanted an explanation is because as someone new to programming I see a bunch of stuff thatâs complex for me. For example a list comprehension looks like it has so much going on (this in that in this in that in this in that). I didnât know a value could be on the left side. I like that this solution is close to how weâd say it in English.