Search code examples
pythonlistreplacestore

Store and Replace the Word from a List (Python)


I'm trying to store a word that is in a list and that word is to be replaced with another. The word that is stored, the user will try to guess what the word is.

print("\nPlease enter the name of the file.")
filename = input()
sample=open(filename, 'r').read()
print(sample)

import random

with open ("words.txt") as w:
    words = random.sample ([x.rstrip() for x in w],9)
    grid = [words[i:i + 3] for i in range (0, len(words), 3)]
    for a,b,c in grid:
        print(a,b,c)

import time,os

time.sleep(5)
os.system('clear')

So what happens in the above is that the user enters the file that contains ten different words. The different words will be arranged in a grid.A timer begins where the user needs to memorize the words in that list and once the timer is up, another grid will appear where one of the words has been replaced. The user then needs to input the word that has been replaced.

What I'm struggling with is actually storing the word from the list that is going to be replaced so that the user can correctly enter the replaced word.

I've looked at replacing words in lists and whatnot but I am unable to find an answer to storing the word that is going to be replaced.

Thanks


Solution

  • To answer your question, here is an example of how to replace a string and then use it in your program. This short example just displays a list of nine letters where one has been replaced with x. The user must input the letter that should be where the x is.

    import random
    
    l = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
    
    replace = random.choice(l)
    l[l.index(replace)] = 'x'
    
    print(l)
    i = input("Which letter was replaced? ")
    
    if i == replace:
        print("That's right!")
    else:
        print("Nope, it was {} that got replaced!".format(replace))
    

    It's somewhat difficult to tell what's going on in your code, so I rewrote the program in a different style. You should be able to incorporate some of these techniques into your own code. /usr/share/dict/words is a standard Linux file that contains a list of dictionary words on separate lines.

    import os
    import random
    import time
    
    def print_grid(li):
        grid = [word_list[i:i+3] for i in range(0, len(word_list), 3)]
        for l in grid:
            print(''.join('{:23}'.format(i) for i in l))
    
    if __name__ == "__main__":
        # get words
        with open('/usr/share/dict/words') as f:
            words = f.read().splitlines()
    
        # get nine random words, the word to replace, and a new word to replace it
        word_list = [random.choice(words) for _ in range(9)]
        replace_word = random.choice(word_list)
        new_word = random.choice(words)
    
        # print words in a grid, wait 5 seconds, and clear the screen
        print_grid(word_list)
        time.sleep(5)
        os.system('clear')
    
        # replace word, and print modified list in a grid
        word_list[word_list.index(replace_word)] = new_word
        print_grid(word_list)
    
        # prompt user to guess
        guess = input("Which word has been replaced? ")
    
        # compare the guess to the replaced word
        if guess == replace_word:
            print("That's right!")
        else:
            print("Incorrect, {} had been replaced.".format(replace_word))