Close Menu

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    What's Hot

    Windows 10 End of Service: What Must Be Done

    19 March 2025

    Elementor #7217

    5 March 2025

    Why Windows is Still the Best for Gamers: A Deep Dive

    27 February 2025
    Facebook X (Twitter) Instagram
    Facebook X (Twitter) Instagram Vimeo
    Let's Tech It Easy
    Subscribe Login
    • Homepage
    • About
    • Blog
      • Computers
      • Cloud
      • Gaming
      • Cyber Security
      • iPhone
      • Mac
      • Windows
      • Android
    • Contact
    • My Tickets
    • Submit Ticket
    Let's Tech It Easy
    Home»Computers»Let’s Play with Python!! ?
    Computers

    Let’s Play with Python!! ?

    ReshitaBy Reshita26 December 2020No Comments6 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr WhatsApp VKontakte Email
    Share
    Facebook Twitter LinkedIn Pinterest Email

    PROJECT 1: A DICTIONARY

    Step 1: Create a folder where you want to keep all the files of this project.

    Step2: Download your dataset for the dictionary

    Step 3: Let’s jump into some coding

    1. As our dictionary data is in JSON format, we will use JSON Standard Library to retrieve the data.

    import json

    You can also use help command in the python cmd to know what each method is used for.

    help(json.load)

    2. Now we will create an object to store the data of the JSON file. Here, my file name is data.json

    data = json.load(open("data.json"))

    If you want to check the type of the object created, use this command

    type(data)

    3. Print the data and check if we are on the right path

    print(data)
    print(data["rain"])

    4. Let’s combine the above written code and create a flow

    import json
    
    data = json.load(open("data.json"))
    
    def definition(inp):  #function to return the desired data
        return data[inp]
    
    word = input("Please enter your Word here: ")   
    #takes an input from the user
    
    word2 = str(definition(word))   
    #converts the list object to string object
    
    print("It's meaning: " + word2) 
    # prints the concatenation of both strings

    5. It is a good practice to create an application by solving one issue at a time.

    ISSUE 1 : The program ends if wrong spelling or incorrect word is entered by the user

    SOLUTION : We will include if-else statement which will check if the word exists in the dictionary or not

    import json
    
    data = json.load(open("data.json"))
    
    def definition(inp):  
        if inp in data:
            return data[inp]
        else:
            return "The word does not exist"
    
    word = input("Please enter your Word here: ")   
    word2 = str(definition(word))   
    print("It's meaning: " + word2) 

    ISSUE 2: If the input word is in uppercase or a combination of cases, the program throws an error

    SOLUTION: As all the data in the JSON file is in lower case, we will convert our input into lowercase before it enters the function

    import json
    
    data = json.load(open("data.json"))
    
    def definition(inp):  
        if inp in data:
            return data[inp]
        else:
            return "The word does not exist"
    
    word = input("Please enter your Word here: ")  
    word2 = str(definition(word.lower()))  
    print("It's meaning: " + word2)

    LET’S MAKE OUR PROGRAM MORE INTELLIGENT!!

    ISSUE 3: What if the user doesn’t know the correct spelling and mistyped the word

    SOLUTION: we will use a special library “difflib”

    To know more about difflib and it’s methods, use the following code in the python cmd

    import difflib
    from difflib import get_close_matches
    help(get_close_matches)
    import json
    import difflib
    
    data = json.load(open("data.json"))
    
    def definition(inp):  
        if inp in data:
            return data[inp]
        elif len(difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)) > 0:  
            i = input("Did you mean %s ? Press Y for Yes and N for NO  " % difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)[0])
            d = difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)[0]
            if i == "y" or i == "Y":
                return data[d]
            elif i == "n" or i == "N": 
                return "Please double check the word and try again"
            else:
                return "Invalid input!!"
        else:
            return "The word does not exist"
    
    user_word = input("Please enter your Word here: ")  
    meaning_word = definition(user_word.lower())
    print(meaning_word)

    ISSUE 4: If there is more than one meaning of a word, we get a list of all meanings together as the output

    SOLUTION: We can use for loop to print each list item in separate lines

    import json
    import difflib
    
    data = json.load(open("data.json"))
    
    def definition(inp):  
        if inp in data:
            return data[inp]
        elif len(difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)) >0:  
            i = input("Did you mean %s ? Press Y for Yes and N for NO  " % difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)[0])
            d = difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)[0]
            if i == "y" or i == "Y":
                return data[d]
            elif i == "n" or i == "N": 
                return "Please double check the word and try again"
            else:
                return "Invalid input!!"
        else:
            return "The word does not exist"
    
    user_word = input("Please enter your Word here: ")  
    meaning_word = definition(user_word.lower())  
    
    for mw in meaning_word: #to distinguish each meaning of the word
        print(mw) 
    

    ISSUE 5: Whenever we press N, return gives us a string. The for loop prints out each letter of that string as the output (vertically). That means sometimes we get a string and sometimes we get a list as the output

    SOLUTION: We can use if-else statement to check the type of the output generated

    import json
    import difflib
    
    data = json.load(open("data.json"))
    
    def definition(inp):  
        if inp in data:
            return data[inp]
        elif len(difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)) >0:  
            i = input("Did you mean %s ? Press Y for Yes and N for NO  " % difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)[0])
            d = difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)[0]
            if i == "y" or i == "Y":
                return data[d]
            elif i == "n" or i == "N": 
                return "Please double check the word and try again"
            else:
                return "Invalid input!!"
        else:
            return "The word does not exist"
    
    user_word = input("Please enter your Word here: ")   
    meaning_word = definition(user_word.lower())  
    
    if type(meaning_word) == list:
        for mw in meaning_word: #to distinguish each meaning of the word
            print(mw) 
    else:
        print(meaning_word)
    

    ISSUE 6: If any common noun such as city names are given as input, the program says that the word does not exist.

    SOLUTION: This is because the dictionary has saved the city names with first letter capital. Therefore, we will use title() method as another if-else condition to check such kind of words in the dictionary.

    import json
    import difflib
    
    data = json.load(open("data.json"))
    
    def definition(inp): 
        if inp in data:
            return data[inp]
        elif inp.title() in data:
            return data[inp.title()]
        elif len(difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)) >0:  
            i = input("Did you mean %s ? Press Y for Yes and N for NO  " % difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)[0])
            d = difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)[0]
            if i == "y" or i == "Y":
                return data[d]
            elif i == "n" or i == "N": 
                return "Please double check the word and try again"
            else:
                return "Invalid input!!"
        else:
            return "The word does not exist"
    
    user_word = input("Please enter your Word here: ")  
    meaning_word = definition(user_word.lower())  
    
    if type(meaning_word) == list:
        for mw in meaning_word:
            print(mw) 
    else:
        print(meaning_word)
    
    

    ISSUE 7: What if acronyms are entered such as USA ? the program cannot identify such inputs

    SOLUTION: Similar to the above issue, we will use upper() method to identify such words

    import json
    import difflib
    
    data = json.load(open("data.json"))
    
    def definition(inp):  
        if inp in data:
            return data[inp]
        elif inp.title() in data:
            return data[inp.title()]
        elif inp.upper() in data: 
            return data[inp.upper()]
    
        elif len(difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)) >0:  
            i = input("Did you mean %s ? Press Y for Yes and N for NO  " % difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)[0])
            d = difflib.get_close_matches(inp,data.keys(),n=2,cutoff=0.5)[0]
            if i == "y" or i == "Y":
                return data[d]
            elif i == "n" or i == "N": 
                return "Please double check the word and try again"
            else:
                return "Invalid input!!"
        else:
            return "The word does not exist"
    
    user_word = input("Please enter your Word here: ") 
    meaning_word = definition(user_word.lower())  
    
    if type(meaning_word) == list:
        for mw in meaning_word:
            print(mw) 
    else:
        print(meaning_word)
    
    

    Andddd…. FINALLY OUR FIRST PROJECT IS COMPLETE!!

    CONGRATULATIONS!! YOU DID A GREAT JOB ??

    Share. Facebook Twitter Pinterest LinkedIn Tumblr WhatsApp Email
    Previous ArticleDoS Attack Implementation and Prevention in Ubuntu
    Next Article Virus Implementation in Kali Linux and Prevention
    Reshita

    Related Posts

    Windows 10 End of Service: What Must Be Done

    19 March 2025

    Elementor #7217

    5 March 2025

    Why Windows is Still the Best for Gamers: A Deep Dive

    27 February 2025

    Accessing a Windows External Hard Drive on Mac

    26 February 2025
    Leave A Reply Cancel Reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    Demo
    Our Picks
    • Facebook
    • Twitter
    • Pinterest
    • Instagram
    • YouTube
    • Vimeo
    Don't Miss
    Business

    Windows 10 End of Service: What Must Be Done

    By Uneeb19 March 20250

    On October 14, 2025, Microsoft will officially end support for Windows 10, signalling a major shift…

    Elementor #7217

    5 March 2025

    Why Windows is Still the Best for Gamers: A Deep Dive

    27 February 2025

    Accessing a Windows External Hard Drive on Mac

    26 February 2025

    Subscribe to Updates

    Get the latest creative news from SmartMag about art & design.

    You too can join us

    If you also think about technology and want to contribute either as a mentor or even from a learner’s perspective, look no further and join us. Write us at [email protected] and share your opinion. Our team will get back by sending you an invite to join the platform as a contributor. Empower others, empower yourself so each one of us can play with the technology safely one day without being scared.

    Subscribe Here
    Loading
    For Partnership Worldwide

    Contact:

    [email protected]

     

    About Us
    About Us

    “Let’s Tech It Easy” or popularly known as “LTIE” is the blogging platform for everyone who wants to share and learn about technology. It is an initiative by the serial techpreneur Vish when he realized the wide gap between the pace at which the technology is evolving and at which it is getting adopted by a wider audience.

    Email Us: [email protected]

    Latest Posts

    Upgrading RAM

    10 March 2023

    Desktop Vs Laptop

    10 March 2023

    Data Recovery

    3 March 2023

    MacOS on Windows Virtual Box

    10 February 2023

    macOS Monterey and what’s new in it?

    12 April 2022
    New Comments
    • How to Troubleshoot Sound and Mic on Windows 10 - Let's Tech It Easy on How to Access Troubleshooters on Windows 10
    • How to Stay Safe While Using Public Wi-Fi Networks - Let's Tech It Easy on Internet Security for Home Users – VPN 101
    • How to Set up Oracle VirtualBox on a Mac - Let's Tech It Easy on How to Install Windows 10 on a Mac Using Boot Camp Assistant
    • DoS Attack Implementation and Prevention in Ubuntu – Let's Tech It Easy on Top Kali Linux Commands
    Facebook X (Twitter) Instagram Pinterest
    • Homepage
    • About
    • Blog
    • Contact
    • Computers
    • Cloud
    • Gaming
    • Cyber Security
    • iPhone
    • Mac
    • Windows
    • My Tickets
    • Submit Ticket
    © 2025 LetsTechitEasy. Designed by Sukrit Infotech.

    Type above and press Enter to search. Press Esc to cancel.

    Sign In or Register

    Welcome Back!

    Login below or Register Now.

    Lost password?

    Register Now!

    Already registered? Login.

    A password will be e-mailed to you.