回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>出于学习的原因,我正在尝试创建一个琐事游戏,但我的字典发生了一些奇怪的事情,我无法理解</p>
<p>我通常测试的代码是:</p>
<pre><code>cat = {
'easy': {
'books': 'https://opentdb.com/api.php?amount=50&category=10&difficulty=easy&type=boolean',
'films': 'https://opentdb.com/api.php?amount=50&category=11&difficulty=easy&type=boolean',
'general knowledge': 'https://opentdb.com/api.php?amount=50&category=9&difficulty=easy&type=boolean',
'music': 'https://opentdb.com/api.php?amount=50&category=12&difficulty=easy&type=boolean',
'science & nature': 'https://opentdb.com/api.php?amount=50&category=17&difficulty=easy&type=boolean',
'sports': 'https://opentdb.com/api.php?amount=50&category=21&difficulty=easy&type=boolean'
},
'medium': {
'books': 'https://opentdb.com/api.php?amount=50&category=10&difficulty=medium&type=boolean',
'films': 'https://opentdb.com/api.php?amount=50&category=11&difficulty=medium&type=boolean',
'general knowledge': 'https://opentdb.com/api.php?amount=50&category=9&difficulty=medium&type=boolean',
'music': 'https://opentdb.com/api.php?amount=50&category=12&difficulty=medium&type=boolean',
'science & nature': 'https://opentdb.com/api.php?amount=50&category=17&difficulty=medium&type=boolean',
'sports': 'https://opentdb.com/api.php?amount=50&category=21&difficulty=medium&type=boolean'
}
}
print(cat)
for level in cat:
print(level)
catselect = []
while catselect not in ("1", "2"):
catselect = input("Select a category, for easy press 1, for medium press 2: ")
if catselect == "1":
selectedcat = "easy"
elif catselect == "2":
selectedcat = "medium"
print(f"You selected the {selectedcat} difficulty level")
print("The subjects can be: ")
for i, cat[selectedcat] in enumerate(cat[selectedcat]):
print(i, cat[selectedcat])
print(cat)
</code></pre>
<p>因此,当代码运行到最后时,cat字典与其他字典并不相同
更多,我没有任何理由认为这可能发生</p>
<p>这就是我所看到的:</p>
<pre><code>{'easy': {'books': 'https://opentdb.com/api.php?amount=50&category=10&difficulty=easy&type=boolean', 'films': 'https://opentdb.com/api.php?amount=50&category=11&difficulty=easy&type=boolean', 'general knowledge': 'https://opentdb.com/api.php?amount=50&category=9&difficulty=easy&type=boolean', 'music': 'https://opentdb.com/api.php?amount=50&category=12&difficulty=easy&type=boolean', 'science & nature': 'https://opentdb.com/api.php?amount=50&category=17&difficulty=easy&type=boolean', 'sports': 'https://opentdb.com/api.php?amount=50&category=21&difficulty=easy&type=boolean'}, 'medium': {'books': 'https://opentdb.com/api.php?amount=50&category=10&difficulty=medium&type=boolean', 'films': 'https://opentdb.com/api.php?amount=50&category=11&difficulty=medium&type=boolean', 'general knowledge': 'https://opentdb.com/api.php?amount=50&category=9&difficulty=medium&type=boolean', 'music': 'https://opentdb.com/api.php?amount=50&category=12&difficulty=medium&type=boolean', 'science & nature': 'https://opentdb.com/api.php?amount=50&category=17&difficulty=medium&type=boolean', 'sports': 'https://opentdb.com/api.php?amount=50&category=21&difficulty=medium&type=boolean'}}
easy
medium
Select a category, for easy press 1, for medium press 2: 1
You selected the easy difficulty level
The subjects can be:
0 general knowledge
1 books
2 films
3 music
4 sports
5 science & nature
{'easy': 'science & nature', 'medium': {'general knowledge': 'https://opentdb.com/api.php?amount=50&category=9&difficulty=medium&type=boolean', 'books': 'https://opentdb.com/api.php?amount=50&category=10&difficulty=medium&type=boolean', 'films': 'https://opentdb.com/api.php?amount=50&category=11&difficulty=medium&type=boolean', 'music': 'https://opentdb.com/api.php?amount=50&category=12&difficulty=medium&type=boolean', 'sports': 'https://opentdb.com/api.php?amount=50&category=21&difficulty=medium&type=boolean', 'science & nature': 'https://opentdb.com/api.php?amount=50&category=17&difficulty=medium&type=boolean'}}
</code></pre>
<p>所有<code>easy</code>的类别都到哪里去了?为什么我的结局是<code>'science & nature'</code></p>