Мне нужно определить имена функций Correct(), которые имеют два параметра. Список строк, которые будут проверяться на слова с ошибками (этот параметр будет моим файлом), и мой словарь, который я уже сделал, называется mydictionary. Функция должна проверять каждое слово в первом списке, используя словарь и функцию check word(). правильный должен вернуть список слов со всеми словами с орфографической ошибкой. Например, правильный (['the', 'cheif', 'stopped', 'the', 'theif']) должен возвращать список ['' '', 'chief', 'stopped', 'the', ' «вор»] Тогда я должен проверить функцию в главном()Использование словарей в python
import string
# Makes a function to read a file, use empty {} to create an empty dict
# then reads the file by using a for loop
def make_dict():
file = open ("spellingWords.txt")
dictionary = {}
for line in file:
# Splits the lines in the file as the keys and values, and assigning them as
# misspell and spell
misspell, spell = string.split(line.strip())
# Assigns the key to the value
dictionary[misspell] = spell
file.close()
return dictionary
mydictionary = make_dict()
#print mydictionary
# Gets an input from the user
word = raw_input("Enter word")
# Uses the dictionary and the input as the parameters
def check_word(word,mydictionary):
# Uses an if statement to check to see if the misspelled word is in the
# dictionary
if word in mydictionary:
return mydictionary[word]
# If it is not in the dictionary then it will return the word
else:
return word
# Prints the function
print check_word(word,mydictionary)
def main():
file2 = open ("paragraph.txt")
file2.read()
thelist = string.split(file2)
print thelist
def correct(file2,mydictionary):
return thelist
paragraph = string.join(thelist)
print paragraph
main()
Все мои другие функции работают, кроме моей правильной функции() и моей функции Main(). Это также дает мне ошибку «файловый объект не имеет атрибута split». Могу ли я получить помощь в своих ошибках?
поэтому я исправил и теперь моей главной функции
def main():
file2 = open ("paragraph.txt")
lines = file2.read()
thelist = string.split(lines)
def correct(file2,mydictionary):
while thelist in mydictionary:
return mydictionary[thelist]
paragraph = string.join(thelist)
print correct(file2,mydictionary)
однако теперь я получаю ошибку «unhashable типа:„список“»