如何用python制作语言词典?
我希望用户能够插入英语单词并返回西班牙语。这就是我所拥有的:
选择 | 换行 | 行号
- # Two arrays, one for english words, the other for their spanish hit.
- english = [ ]
- spanish = [ ]
- # The words and definitions are in a file. English word first then tab and then Spanish definition on one line. Then add the words in their respective arrays.
- file=open("eng-span.txt", "r")
- while True :
- # Read one line
- line = file.readline()
- # Check, if it's the last line
- if line == "" :
- break
- word_pair = line.split ("\t")
- english.append (word_pair[0])
- spanish.append (word_pair[1])
- print " Enter the English word for which you want the Spanish definition to: "
- word = raw_input()
- # Tricky part for me: a for-cycle that checks whether the inserted word exists in the array 'english' and then prints the corresponding Spanish definition from array 'spanish'. I have the cycle all wrong...
- for i in english:
- if word in english :
- print spanish[ i ]
- else :
- print "No definition"