Challenge

Project 1 English Dictionary App

import json

# to print the words from the app
def output():
    f = open('words.txt', 'r+') # opening the file in read mode
    print('file opened')

    lines =  f.readlines() # reading all the lines of the file in list called lines

    for line in lines:
        print(line, end = ' ')# iterate through the listone item at a time
        #print(len(line))
            
        
    print('\n')
    print('\n')
    print('\n')
    
    f.close()
    
    
# item1 to add word to the dictionary
def item1():
    f = open('words.txt', 'a')
    word = input('enter the word of your choice :')
    mean = input('enter the meaning :')
    student = {word : mean}
    json.dump(student, f)
    f.write("\n")

    f.close()
    
    
    printinput = input('Do you want to print the output then give your command whether y or n:')
    
    if printinput == 'y':
        output()
    else:
        pass    




# item2 to search the word in the dictonary
def item2():
    x = input('enter the word: ')
    with open("words.txt") as openfile:
        flag = False
        for line in openfile:
            line = json.loads(line.strip())
            if x in line.keys():
                flag = True
                if len(line[x].strip())> 0:
                    print(line[x])
            
                    
        
        if not flag:
            print("no such word exits \n")
            print("so no meaning found \n")
                



# item3 to update the word

def item3():
    print('the words in the dictionary are: ')
    print('\n')
    print('\n')
    output()
    print('\n')
    print('\n')
    print('the word to be updated is: ')
    print('\n')
    
    x = input('enter the word: ')
    mean = input('enter the meaning: ')
     
    with open("words.txt") as openfile:
        lines = []
        for line in openfile:
            line = json.loads(line.strip())
            lines.append(line)
            if x in line.keys():
                line[x] = mean
                print(line[x])
                
    with open("words.txt", 'w') as f:
        for line in lines:
            json.dump(line, f)
            f.write('\n')
            
    print('the updated dictionary is: ')
    print('\n')
    
    output()
    
    print('\n')
    



# a start program
def start(j):
    if j == 1:
        print('welcome to the dictionary app')
        print('please provide your input')
        print('the choices are:')
        print('item 1 : you can add your word and meaning. If so write 1 as input \n'
              'item 2: you can search the word and meaning. If so write 2 as input \n'
              'item 3: you can update the meaning of the word. If so write 3 as input \n'
              'item 4: exit from the app. If so write 4 as input \n')
        
        y = int(input('enter the number between 1 to 4 of your choice : '))
        
        print('\n')
        print('\n')
    
    
        while y > 0 and y < 5:
            if y == 1:
                item1()
            elif y == 2:
                item2()
            elif y == 3:
                item3()
            else:
                break
            
            y = int(input('enter the number :'))
            
    else:
        return 'sorry end of the program'


# driver code

j = int(input('give your input if 1 to start the program or 0 to end the program :'))
print('\n')
print('\n')

print(start(j))


Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.