1

This is the question:

enter image description here

and this the output that is required:

enter image description here

This is code I have written so far:

class_list = []
keys = []
meanings = []

def script():
    key = input("Enter the word: ")
    no_meanings = int(input("\nEnter the no of meanings: "))
    meanings.append(no_meanings)
    keys.append(key)
    print(keys)
    print(meanings)

print("\nEnter the meanings: ")
for i in range(no_meanings):
    listVal = input()
    class_list.append(listVal)
    text = '\n'.join(class_list)

if(no_meanings <= 0):
    print("Invalid Input")
    if(len(keys) == 1):
        print("Here's the dictionary you've created: " )
        for i in range(len(keys)):
            for i in range((len(meanings))):
                print(keys[i] + ":" + str(class_list[:int(meanings[i])]))
                del class_list[:int(meanings[i])]
    else:
        print("Here's the dictionary you've created: " )
        for i in range(len(keys)):
            for x in range((len(meanings)//2)):
                print(keys[i] + ":" + str(class_list[:int(meanings[x])]))
                del class_list[:int(meanings[x])]

try:
    restart = input("Do you want to add one more elements to the dictionary? If yes, press 1, else press 0: \n")
except EOFError:
    exit()

if restart == "1":
    script()
elif restart == "0":
    if(len(keys) == 1):
        print("Here's the dictionary you've created: " )
        for i in range(len(keys)):
            for i in range((len(meanings))):
                print(keys[i] + ":" + str(class_list[:no_meanings]))
                del class_list[:no_meanings]
    else:
        print("Here's the dictionary you've created: " )
        for i in range(len(keys)):
            for x in range((len(meanings)//2)):
                print(keys[i] + ":" + str(class_list[:no_meanings]))
                del class_list[:no_meanings]


else:
    print("Invalid Input")
    print("Here's the dictionary you've created: ")
    for i in range(len(keys)):
        for i in range((len(meanings))):
            print(keys[i] + ": " + str(class_list[:no_meanings]))
            del class_list[:no_meanings]
script()

This code doesn't works well when i keep on adding the words and meanings but when I enter no. of meanings as "0". It doesn't print the previously entered words and meanings.

I am a beginner to the programming world and have been working on this for DAYSSS but i still can't able to figure out the logic.

carlos
  • 45
  • 4

1 Answers1

0

This is the way I would solve the question:

def print_dict(word_dict):
    print("Here is the dictionary you've created: ")
    for key in word_dict:
        print(key, ":", word_dict[key])

def restart_prompt(word_dict):
    restart = input("Do you want to add another entry into the dictionary? If yes press 1, else press 0 ")
    while restart not in ["0","1"]:
        print("Invalid Input, try again: ")
        restart = input("Do you want to add another entry into the dictionary? If yes press 1, else press 0 ")
    return int(restart)

def script_(n):

    word_dictionary = {}
    restart = 1
    num_words_in_dict = 0

    while restart == 1 and num_words_in_dict<n:
        word = input("Enter the word: ")
        num_meanings = input("Enter the number of meanings: ")
        while not num_meanings.isdigit():
            print("Invalid Input, try again")
            num_meanings = input("Enter the number of meanings: ")

        meanings = []
        print("Enter the meanings: ")
        for entry in range(int(num_meanings)):
            meanings.append(input())

        word_dictionary[word] = meanings
        num_words_in_dict+=1

        if num_words_in_dict < n:
            restart = restart_prompt(word_dictionary)

    print_dict(word_dictionary)


There are several improvements made in the code above:

  1. An actual dictionary is used, which allows there to be fewer containers that need to be maintained. This also makes printing much much simpler.
  2. The code is cleaner because it is split up into smaller more focused functions
  3. The dictionary stops being filled after n words have been inserted
  4. When the user inputs an invalid number for num_meanings, they are prompted again until a valid input is given
  5. There is no recursion, using recursion here is unnecessary and can lead to confusing problems that are hard to debug.

Another improvement that can be made is that if the user inputs a word that is already in the dictionary, then it will overwrite the previous definition, so you might want to put in some input check that only allows the user to insert new words.

I hope that this code follows the instructions exactly as needed, but they are quite unclear.

PurpleHacker
  • 358
  • 2
  • 9