-5

I would like to create a program in Python 3 which allows the user to enter the amount of letters the word has, and the some letters.

For instance:

>> Input how many letters there are in the word
> 5

>> Put _ if no letter is shown, and letter that is shown down
> _ell_

>> Possible finds: Hello, Mello
>> Update the search
> Hell_

>> Final find: Hello
>> Restart?:
> Yes

I don't really know how to explain this in proper words, but you are developers as said so I'm sure you understand that.

You let the user input the amount of letters that are in the word. Then you let the user input _ as blank letters and proper letters as they are shown (st_ing >> string) Then it will come up with some words that match that search that is from a dictionary array or a text file (By array I mean words = ["word1", "word2", word3"] etc..) Then you can type to narrow the search if there are no more than 1 find Once there is only 1 find, it will prompt to restart, and then yes = restart.

I'm new to python so this is probably the most complicated for me, that is why I'm asking you!

I'm asking how complicated would this be and if it is even possible, how would I go about it. I have started it, here is all I have right now: (Keep in mind I just started)

two_word = ["hi", "ai", "no", "id"]
three_word = ["run", "buy", "tie", "bit", "fat"]
four_word = ["help", "file", "edit", "code", "user"]
five_word = ["couch", "cough", "coach", "stars", "spoon", "sunny", "maths"]

letter_count = input("How much letters are there?: ")
letter_count = int(letter_count)

if letter_count == 2:
    wordlist = two_word

elif letter_count == 3:
    wordlist = three_word

elif letter_count == 4:
    wordlist = four_word

elif letter_count == 5:
    wordlist = five_word

else:
    print("Improper entry.")


guess = input("The word right now: ")

blanks = guess.count("_")
#I don't know how to check for certain letters and how to convert _ to the word in the wordlist
#That is why I'm asking
Introzoid
  • 15
  • 4
  • 1
    You haven't really described what the problem you're facing so far is, you've just described what you're trying to do. – Dimitris Fasarakis Hilliard Nov 29 '16 at 19:16
  • 1
    What have you come up with yourself so far? – vintastic Nov 29 '16 at 19:17
  • "but you are developers as said so I'm sure you understand that." We aren't here to write code for you unless you've shown meaningful effort on your behalf. StackOverflow isn't a code-writing service. – blacksite Nov 29 '16 at 19:18
  • I can write the code for you. How much are you willing to pay me? :P Jokes apart. SO is not on-demand coding service, we may only help you if your question shows some effort. We can work together on fixing the issues you face, not solving your entire assignment – Moinuddin Quadri Nov 29 '16 at 19:20
  • I added my attempt, which is bad as I just started, whereas its probably stupid code to you professionals! Please tell me how to do the things in the hash – Introzoid Nov 29 '16 at 19:45
  • @JimFasarakis-Hilliard – Introzoid Nov 29 '16 at 20:18
  • Possible duplicate of [How to check if an element exists in a Python array (Equivalent of PHP in\_array)?](http://stackoverflow.com/questions/14743156/how-to-check-if-an-element-exists-in-a-python-array-equivalent-of-php-in-array) – Eli Sadoff Nov 30 '16 at 03:13

1 Answers1

0

If I was developing this, I would:

  • Keep all the words in a single set
  • Skip the first question (you can determine word length by the length of the 2nd question)
  • Set up a while loop for each question you ask the user so that it repeats the same question on invalid input.

To check for the word, you could compile a regular expression and replace all _'s with .s:

regex = re.compile(guess.replace('_', '.') + '$')

Now the part you've been waiting for, checking if an item in the set matches:

match = [m.group(0) for word in wordlist for m in [regex.match(word)] if m]
print(' '.join(match) or "No matches")

The list comprehension above basically iterates through every word in the list (which can be prefiltered by length if you prefer), then checks it to see if it matches the regular expression created earlier. If it's a match, m will be a Match Object that you can get the first group of which will be the word you are looking for all packaged together as a list of words that match.

The last line prints all the matches separated by a space, or "No matches" if there isn't any matches.

This code is untested, and I'm not familiar with Python 3 as I am with Python 2. Good luck!

Sunny Patel
  • 7,830
  • 2
  • 31
  • 46
  • When I put this into the code, it releases a syntax error (I think) `regex = re.compile(guess.replace('_', '.') + '$') NameError: name 're' is not defined` I am using Python 3.3.0 – Introzoid Nov 30 '16 at 08:42
  • You need to `import re`, my friend. Afterall, it is a library :) – Sunny Patel Nov 30 '16 at 13:47
  • That just makes another error. – Introzoid Nov 30 '16 at 15:38
  • `Traceback (most recent call last): File "C:/Users/Jone/Desktop/Word.py", line 29, in match = [m.group(0) for word in wordlist for m in [regex.match()] if m] File "C:/Users/Jone/Desktop/Word.py", line 29, in match = [m.group(0) for word in wordlist for m in [regex.match()] if m] TypeError: Required argument 'string' (pos 1) not found` – Introzoid Nov 30 '16 at 16:24
  • Sorry, the `regex.match` was missing the first parameter... `word`. Updated my answer. – Sunny Patel Nov 30 '16 at 17:32
  • A simple Python2 implementation can be found [here](https://repl.it/EbxG). – Sunny Patel Nov 30 '16 at 17:36
  • Great! I don't really understand, but I read the links you put and I somewhat understand that new editions! Thanks! – Introzoid Nov 30 '16 at 19:33