Re: [問題] 出現unhashable type: 'list'該如何處理

看板Python作者 (LCH)時間6年前 (2019/03/22 11:56), 編輯推噓3(301)
留言4則, 2人參與, 6年前最新討論串2/2 (看更多)
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)) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 125.227.45.150 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1553227009.A.DAF.html

03/22 13:35, 6年前 , 1F
真的非常感謝您的精闢詳答,真的非常感恩。
03/22 13:35, 1F

03/22 13:36, 6年前 , 2F
我會努力把它弄懂的。
03/22 13:36, 2F

03/22 16:32, 6年前 , 3F
我覺得correct + incorrect == 5就break阿
03/22 16:32, 3F

03/22 17:14, 6年前 , 4F
好像也是ㄧ種方式,謝謝您
03/22 17:14, 4F
文章代碼(AID): #1Sb5q1sl (Python)
文章代碼(AID): #1Sb5q1sl (Python)