-4

I have been making a simple Hangman game in python. I want a block of code that can determine if the guess of the user (the input) is missing one letter or is different by one letter only from the original word.

For example, let's say the word the user has to guess is "apple". If the user wrote "apple" the game will show "You've guessed the word!" if the user entered a completely different word like "orange" the game will display "Wrong! Try again". So far I have been able to make all of this except the part where if the user typed a word like "aple" or "aPble" or "appl" I want the game to display "CLOSE!".

Please note I am 100% new to python programming as well as stack overflow so if I somehow messed up explaining the problem please excuse me :)

salezica
  • 74,081
  • 25
  • 105
  • 166
  • 1
    Please show a [mre] for the specific problem you want to ask about. – mkrieger1 Jul 21 '20 at 22:08
  • Have a look at how fuzzy search works. There's python libraries available to help you out, e.g. https://pypi.org/project/fuzzysearch/ – Simon S. Jul 21 '20 at 22:09
  • Come on. The question is clear to any human being equipped with functional common sense. Don't downvote, don't scare new people away. – salezica Jul 21 '20 at 22:10

2 Answers2

2

You can do better! Calculate the Levenshtein distance, also known as edit distance.

The what?

The Levenshtein distance is a measure of text similarity. It tells you how many changes you need to go from one piece of text to another, in terms of additions / deletions / replacements.

The words car and bar have a distance of 1, since 1 replacement is enough. Same for tea and team, 1 addition will do. To change real into steal, you need a replacement plus an addition, so the distance is 2.

In other words, it quantifies exactly how close you are to guessing a word. A distance of 1 would match your requirement, but you can play with that number and give richer feedback to your players.

But how?

This answer in StackOverflow contains code you can steal, and links to other implementation options.

salezica
  • 74,081
  • 25
  • 105
  • 166
-1
word = ('apple')
guess = input('Guess word: ')
if guess == word:
    print('You have guessed the word!')
if guess == ('appl') or guess == ('aPpl') or guess == ('aple'):
    print('SO CLOSE')