我需要建立一个程序,它打印出在我写的字典中的所有单词,所以我们可以说我写的房子,它能够站出来像字典打印出所有单词的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',所以我不知道如何建立连接...... –