Question: (Python) I can't get the code to output. Help plz. This is the prompt: Tasks Read data from the words.txt text file and store it

(Python) I can't get the code to output. Help plz. This is the prompt:

Tasks

  1. Read data from the words.txt text file and store it as a list of individual words. Hint: split the contents of the text file on the new line character

  2. Clean your data

    1. Make sure that all the words are in lower case

    2. Remove preceding and trailing spaces

    3. Remove the new line character

    4. Remove duplicates. Hint: convert your list to a set and back to a list

  3. Integrate the following function in your code.

def word_signature(word):

sorted_word = sorted(word)

word_letters = ''.join(sorted_word)

return word_letters

This function determines a words signature. Python basically treats strings as lists of characters, so a words signature is just a string that contains all of the words letters sorted in alphabetical order. For example, the words lives and elvis have the same signature: eilsv.

  1. Ask the user to input a word

  2. Use the word_signature function to get the input words signature

  3. Iterate (loop) through the list of words in the English language

    1. If a signature of the input word matches signatures of any of the words in your list of words, you have your anagrams.

    2. Print the matched word

  4. In the end, your program should print the list of all anagrams for the provided word.

  5. For example, if you search for the word python, your program should find 'typhon' and 'phyton' as the anagrams

(Below is the my code)

_____________________________________________________________

file = open('words.txt').read()

file.split(' ')

text = file.lower()

all_text = text.strip()

wordset = set(all_text)

word = list(wordset)

def word_signature(word):

sorted_word = sorted(word)

word_letters = ''.join(sorted_word)

return word_letters

user = input("Enter word: ")

input_signature = word_letters(user)

for word in word:

if word_signature(word) == input_signature:

print(str(word))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!