[Python] Thesaurus Lab Assignment

Discussão em 'Python' iniciado por Stack, Julho 15, 2025.

  1. Stack

    Stack Membro Participativo

    I have a homework assignment that I can't seem to figure out. The problem is:


    Given a set of text files containing synonyms for different words, complete the main program to output the synonyms for a specific word. Each text file contains synonyms for the word specified in the file’s name, and each row within the file lists the word’s synonyms that begin with the same letter, separated by a space. The program reads a word and a letter from the user and opens the text file associated with the input word. The program then stores the contents of the text file into a dictionary predefined in the program. Finally the program searches the dictionary and outputs all the synonyms that begin with the input letter, one synonym per line, or a message if no synonyms that begin with the input letter are found.

    Hints: Use the first letter of a synonym as the key when storing the synonym into the dictionary. Assume all letters are in lowercase.

    Ex: If the input of the program is:

    educate
    c the program opens the file educate.txt, which contains:

    brainwash brief
    civilize coach cultivate
    develop discipline drill
    edify enlighten exercise explain
    foster
    improve indoctrinate inform instruct
    mature
    nurture
    rear
    school
    train tutor then the program outputs:

    civilize
    coach
    cultivate Ex: If the input of the program is:

    educate
    a then the program outputs:

    No synonyms for educate begin with a.​

    I currently have this with only one file being focused right now:

    synonyms = {} # Define dictionary

    initial_input = input()
    start_letter = input()


    if initial_input == 'Educate' or 'educate':
    with open('educate.txt', 'r') as e:
    lines = e.readlines()
    for word in lines:
    if ord(str(word[0])) == ord(str(start_letter)):
    key = start_letter
    split = word.split(' ')
    synonyms[key] = split
    print(synonyms)
    if ord(str(start_letter)) != ord(str(word[0])):
    print(f'No synonyms for educate begin with {start_letter}.')

    elif initial_input == 'Happy' or 'happy':
    with open('happy.txt', 'r') as h:
    lines = h.readlines()


    elif initial_input == 'Goal' or 'goal':
    with open('goal.txt', 'r') as e:
    lines = e.readlines()


    and when inputting:

    educate
    b


    I get:

    {'b': ['brainwash', 'brief\n']}
    No synonyms for educate begin with b.
    No synonyms for educate begin with b.
    No synonyms for educate begin with b.
    No synonyms for educate begin with b.
    No synonyms for educate begin with b.
    No synonyms for educate begin with b.
    No synonyms for educate begin with b.
    No synonyms for educate begin with b.
    No synonyms for educate begin with b.
    No synonyms for educate begin with b.


    I know I can get the values individually but cannot seem to new line each entry by replacing print(synonyms) with print(word), but currently I am at a loss.

    Edit: I have modified the code a little bit and came up with:

    if initial_input.lower() == 'educate':
    with open('educate.txt', 'r') as e:
    lines = e.readlines()
    for word in lines:
    if word[0] == start_letter:
    split = word.split(' ')
    synonyms[start_letter] = split
    for x in synonyms[start_letter]:
    print(x)
    break
    else:
    print(f'No synonyms for educate begin with {start_letter}.')


    but see that since it reads the lines top to bottom, when changing the starting letter to 'c' for example, it outputs:

    No synonyms for educate begin with c.
    civilize
    coach
    cultivate


    How would you make it not look at the letters above?

    Continue reading...

Compartilhe esta Página