Re: [问题] 出现unhashable type: 'list'该如何处理

楼主: as30385438 (LCT)   2019-03-22 11:56:46
dictionary=hash table
python的built-in物件中...
可变的物件都没有__hash__, 如list, dict, set等等
所以不能拿来当字典的key
回到你的问题
从Dict中取出特定数目的问题问user
import random
Dict={"红":"红色","成":"成功","失":"失败","漂":"漂亮","勇":"勇敢",
"天":"天空","写":"写字","力":"力量","走":"走路","手":"手机"}
num = 5
TwoWords=list(Dict.keys())
random.shuffle(TwoWords)
print("\n")
correct = 0
incorrect = 0
for word in TwoWords[:num]:
print(word)
term=input ("请输入词汇:")
if term==Dict[word]:
print("答对了!\n")
correct+=1
else:
print("答错了,应该是%s...\n"%Dict[word])
incorrect+=1
print('你答对%i题,答错%i题!' %(correct, incorrect))
作者: qzxwec789 (Frank)   2019-03-22 13:35:00
真的非常感谢您的精辟详答,真的非常感恩。我会努力把它弄懂的。
作者: lemon651 (小明)   2019-03-22 16:32:00
我觉得correct + incorrect == 5就break阿
作者: qzxwec789 (Frank)   2019-03-22 17:14:00
好像也是ㄧ种方式,谢谢您

Links booklink

Contact Us: admin [ a t ] ucptt.com