出于学习的原因,我正在尝试创建一个琐事游戏,但我的字典发生了一些奇怪的事情,我无法理解
我通常测试的代码是:
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)
因此,当代码运行到最后时,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'}}
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'}}
所有easy
的类别都到哪里去了?为什么我的结局是'science & nature'
问题在于这一行:
每次通过循环,它都会将一个
cat[selectedcat]
元素分配给cat[selectedcat]
,从而修改字典。因此,第一次通过循环时,它确实循环完成后,
cat[selectedcat]
的值将是cat[selectedcat]
的最后一个元素循环变量通常应使用普通变量:
您将在此处重新分配给词典:
您要求
for
循环分配给i
和cat[selectedcat]
。不要那样做上述措施基本上就是这样做的:
因此,将
cat[selectedcat]
中的每个值赋给cat[selectedcat]
本身它恰好起作用,因为
cat[selectedcat]
引用的原始字典仍然被enumerate()
对象引用,因此它的所有键仍然在循环中生成。但是cat
字典本身被要求依次用每个类别字符串替换selectedcat
键的值。您可以看到这种情况,因为您随后在循环中打印cat[selectedcat]
的新值如果要用数字显示值,则需要为循环使用不同的新变量名,例如:
这里,
category
是一个新变量(就像i
一样)相关问题 更多 >
编程相关推荐