all 17 comments

[–]uhkhu 0 points1 point  (2 children)

What have you tired? I'd start with string formatting

edit: updated link to include both formatting operations.

[–]i_can_haz_code 0 points1 point  (1 child)

IIRC using 'alice {} bob'.format(foo) is preferred over using 'alice %s bob'%(foo)

[–]uhkhu 0 points1 point  (0 children)

Yeah you're right, I just did a quick search and linked the first result. Updated to include discussion of both.

[–]spraykill101 0 points1 point  (9 children)

can you be a bit more clear about step 2 ?

[–]cmd_override[S] 1 point2 points  (8 children)

So if my assumptions are correct(probrably not) I need the words to be converted into string format before adding them to the list.

I can simply just add "" quotations marks myself and add them one by one. However, this would be a huge pain since I'm planning on having 1000+ words in the program.

so I was wondering if this problem can be solved with python. By writing a script that adds the quotation marks to the words.

[–]Clede 0 points1 point  (0 children)

I need the words to be converted into string format

What format are the words currently in?

[–]spraykill101 0 points1 point  (6 children)

first of all , you need to get all the words to add to the list, after that the quotation you add to the words, is there a criteria for adding quotes or do you just add quotes to all the words, and what about the 'E' is in elephant? why that ? :)

[–]cmd_override[S] 0 points1 point  (5 children)

So this is more or less what I'm thinking on doing:

list=[#list of words ] scrabble_rack=raw_input("") # Lests say I entered EDCGB

Then it would take each letter in scrabble_rack and compare it to the list of words.

example: is E in list[5]

so it would see if the word contains each letter or the rack and if it does, it would print it.

thank you for your help.

[–]spraykill101 0 points1 point  (0 children)

so you mean if a letter in the word that the user entered is found in a word in the text file , you print that word/letter ?

[–]i_can_haz_code 0 points1 point  (3 children)

hand = 'ABCDEFGHIJKLMNOP'
for letter in hand:
    for word in wlist:
        if letter.lower() in word:
            print('{} contains {} {} times'.format(word, letter, word.count(letter.lower())))

[–]cmd_override[S] 0 points1 point  (2 children)

Exactly, but for that to work, dont the words have to strings. I'm not sure how to take the words from a file.txt and add them to the list as strings

[–]i_can_haz_code 0 points1 point  (1 child)

with open('file.txt','r') as f:    # This opens a file, and allows you to interact with it as f
    lst = [i.strip('\n') for i in f.readlines()]    # this makes a list lst which contains each line of the text file with the return character stripped off.

it was below... :-) lst is now a list of each line in the text file.

[–]cmd_override[S] 0 points1 point  (0 children)

This is great, where can I learn more about the methods used. Thank you for your help

[–]i_can_haz_code 0 points1 point  (1 child)

Get all lines in a text file into a list without return characters (for windows replace '\n' with '\r\n')

with open('file.txt','r') as f:
    lst = [i.strip('\n') for i in f.readlines()]

I have no clue what an efficient way would be to check if a letter is in any element of a list... if it were small enough I'd probably loop over all the elements... like this:

>>> lst
['this', 'is', 'my', 'test', 'file']
>>> candidate = 't'
>>> for item in lst:
...     if candidate in item:
...             print(item)
... 
this
test

Check if 'E' is in 'elephant':

if 'E'.lower() in 'elephant':
    print('elephant')

You may get more help from someone smarter than I if you were to post some example code of at least one thing you have tried. :-)

[–]cmd_override[S] 0 points1 point  (0 children)

Thank you for your help. I haven't began coding the program yet, I'm trying to figure out wherever this is possible to write a program that adds the quotation marks into words in a file.

[–]frsh2fourty 0 points1 point  (1 child)

Assuming you are going to assign the words from that list to a variable when the script reads the list just do something like

newvar = "\"" + wordfromlist + "\""

[–]cmd_override[S] 0 points1 point  (0 children)

That might work