我需要建立一個程序,它打印出在我寫的字典中的所有單詞,所以我們可以說我寫的房子,它能夠站出來像字典打印出所有單詞的I型什麼
家庭,房子,高,中空,出來,過來,高估了,對,團結,無標題,急,歌曲,沉默,沉默,對不起,情感,精靈,雞蛋,結束
所以它得到的所有詞了我的字典與我寫的字,這裏是代碼。
#User types in a word
word = input("Type in a word: ")
#initiate the dictionary struct.
dictionary = {}
#run through the dictionary file, one line at a time.
with open("dict.txt") as dict_filehandle:
for cword in dict_filehandle:
#here a single line has been placed into the variable cword.
#no newlines are allowed in the words.
cword=cword.replace("\n","");
#give the given word the value 1.
dictionary[cword]=1;
#here we have a dictionary struct.
#here we push through all the words in the dictionary
print(dictionary["house"])
在我上面的代碼中,我做了搜索「房子」,它表明,它是通過獲取1號屬實,我不知道該怎麼做我上面說的事情,但我需要能夠做到這一點,因爲這是一個學校項目,我的老師建議我們在互聯網上看,所以我決定只問人。
您的註冊已關閉。你能不能顯示'dict.txt'文件中的內容? –
@ Mr.goosberry https://www.dropbox.com/s/4a1ps1hkl187f8m/dict.txt?dl=0 – Kobbi
我不明白你的問題,你希望用戶輸入'home',你的程序會識別出是指你的字典中的'house'?在你的代碼中沒有使用變量'word',所以我不知道如何建立連接...... –