-1

I'm new to python and am wondering is there a way to take 1 one word from an external file of 10 words and store it individually.

I'm making a words memory game where the user is shown a list of words and then it is removed after a certain amount of time and the words will appear again but one word will be different and they have to guess which word has been replaced.

The word will be randomly chosen from an external file but the external file consists of 10 words, 9 in which will be displayed first and 1 in which is stored as a substitute word.

Does anyone have any ideas?

Peter Wood
  • 23,859
  • 5
  • 60
  • 99
  • 1
    Please try to elaborate with more details. What is the input, expected output and what have you tried so far. – Vikas Ojha Sep 17 '15 at 09:22
  • The 10 words should be taken from an external file and 9 will be randomly chosen to be presented in the first grid and the last word needs to be stored for use later in the program. I've tried putting it into a list but am still unsure how to do it – John Davids Sep 17 '15 at 09:26
  • 1
    There are lots of parts to this. Try the first part without the rest. Convince yourself it works (work out how to convince yourself). Then try the next part. – Peter Wood Sep 17 '15 at 09:27

4 Answers4

1

I have used the unix dictionary here, you can take whichever you want. More resources here:

import random
from copy import copy
''' Word game '''
with open('/usr/share/dict/words','r') as w:
    words = w.read().splitlines()

numWords = 10 
allWords = [words[i] for i in random.sample(range(len(words)),numWords)]

hiddenWord = allWords[0]
displayWords = allWords[1:]

print displayWords

choice = str((raw_input ('Ready? [y]es\n')))
choice = choice.strip()
if choice == 'y':
    indexToRemove = random.randint(0,len(displayWords))

    displayWordsNew = copy(displayWords)
    random.shuffle(displayWordsNew)
    displayWordsNew[indexToRemove] = hiddenWord

    print displayWordsNew
    word = str(raw_input ('Which is the different word\n'))
    if word == displayWordsNew[indexToRemove]:
        print "You got it right"
        print displayWords
        print displayWordsNew
    else:
        print "Oops, you got it wrong, but it's a difficult game! The correct word was"
        print displayWordsNew[indexToRemove]

Results:

["Lena's", 'Galsworthy', 'filliped', 'cadenza', 'telecasts', 'scrutinize', "candidate's", "kayak's", 'workman']
Ready?
y
["Lena's", 'workman', 'scrutinize', 'filliped', 'Latino', 'telecasts', "candidate's", 'cadenza', 'Galsworthy']
Which is the different word
telecasts
Oops, you got it wrong, but it's a difficult game! The correct word was
Latino
Community
  • 1
  • 1
Sahil M
  • 1,790
  • 1
  • 16
  • 31
0

I'm new to python and am wondering is there a way to take 1 one word from an external file of 10 words and store it individually.


There's a LOT of ways to store/reference variables in/from a file.

If you don't mind a little typing, just store the variables in a .py file (remember to use proper python syntax):

 # myconfig.py:

var_a = 'Word1'

var_b = 'Word2'

var_c = 'Word3'

etc...


Use the file itself as a module

from myconfig import *

(This will let you reference all the variables in the text file.)


If you only want to reference individual variables you just import the ones you want

from myconfig import var_a, var_b

(This will let you reference var_a and var_b, but nothing else)

Ajean
  • 5,528
  • 14
  • 46
  • 69
Noah Wood
  • 40
  • 5
  • @Dhruv Somani, I can't comment on yours, but you're incorrect. So long as the files share the same file-path you can import them as modules without having to install them. This is literally the easiest way I can think of to do what the OP wanted. (read variables from a file into memory). – Noah Wood Sep 17 '15 at 11:39
  • but what if he has to access words from a pre-made long file. He would not just make variables for each word. Rather in a text file it gives you more freedom. If they are on separate lines then it is very easy to even edit them. –  Sep 20 '15 at 04:51
  • There's more than one way to skin a rabbit. Your method isn't wrong, it's just complicated, and I doubt the OP is going to know how to manipulate the strings to do what he wants. – Noah Wood Sep 20 '15 at 05:54
0

You should try this:

foo = open("file.txt", mode="r+")

If the words are on different lines:

words = foo.readlines()

Or if the words are separated by spaces:

words = foo.read().split(" ")

Try this...

Noah Wood
  • 40
  • 5
0

If you have an input file like "one word in a new line", just do this:

>>> open("C:/TEXT.txt").read()
'FISH\nMEAT\nWORD\nPLACE\nDOG\n'

Then split the string to the list:

>>> open("C:/Work/TEXT.txt").read().split('\n')
['FISH', 'MEAT', 'WORD', 'PLACE', 'DOG', '']

Oh... And strip new line in the end:

>>> open("C:/Work/TEXT.txt").read().strip().split('\n')
['FISH', 'MEAT', 'WORD', 'PLACE', 'DOG']

For replacing use random.choice from the range of the list:

>>> import random
>>> listOfWords = open("C:/Work/TEXT.txt").read().strip().split('\n')
>>> listOfWords
['FISH', 'MEAT', 'WORD', 'PLACE', 'DOG']
>>> random.choice(range(len(listOfWords)))
3
>>> listOfWords[random.choice(range(len(listOfWords)))] = 'NEW_WORD'
>>> listOfWords
['FISH', 'MEAT', 'NEW_WORD', 'PLACE', 'DOG']

And if you want to shuffle a new list:

>>> random.shuffle(listOfWords)
>>> listOfWords
['PLACE', 'NEW_WORD', 'FISH', 'DOG', 'MEAT']
Sdwdaw
  • 1,037
  • 7
  • 14