python - 优达学城 CS101 : Not sure if this nested dictionary question is wrong or I'm missing something

标签 python dictionary nested

我是 Udacity CS101 上的 Python super 初学者。任务是添加条目 is_noble_gas到现有的嵌套字典,以便它显示 TrueFalse当用括号查找时。

当我提交this时代码来回答这个问题,它告诉我我错了。我是否忽略了什么?

这是测验问题:

测验:向嵌套字典添加值

尝试使用嵌套字典。将另一个条目“is_noble_gas”添加到 elements 中的每个字典中字典。插入新条目后,您应该能够执行这些查找:

>>> print(elements['hydrogen']['is_noble_gas'])
False
>>> print(elements['helium']['is_noble_gas'])
True

这就是我的尝试:

elements = { 
   'hydrogen': {'number': 1, 'weight': 1.00794, 'symbol': 'H',
                'is_noble_gas': 'False'}, 
   'helium': {'number': 2, 'weight': 4.002602, 'symbol': 'He',
              'is_noble_gas': 'True'}
}

最佳答案

事情会是这样的

elements = {'hydrogen': {'number': 1, 'weight': 1.00794, 'symbol': 'H'},
            'helium': {'number': 2, 'weight': 4.002602, 'symbol': 'He'}}

elements["hydrogen"]["is_noble_gas"] = False
elements["helium"]["is_noble_gas"] = True
print(elements['hydrogen']['is_noble_gas'])
print(elements['helium']['is_noble_gas'])

关于python - 优达学城 CS101 : Not sure if this nested dictionary question is wrong or I'm missing something,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59130586/

相关文章:

python - Pandas 数据框中的字典列

javascript - `.map` 不是用于循环吗?

templates - Genshi 嵌套匹配模板

mysql - JSON_REMOVE 嵌套项目,MySQL

python - flask + nginx + uwsgi_response_sendfile_do() 超时

python - psycopg2 aws lambda超时错误

python - Robot Framework 中的拆卸操作

python - 阿克曼函数理解

Python - 计算具有与特定查找字符串匹配的值的字典键的数量

elasticsearch - 如何按嵌套文档数过滤文档