top of page

HANGMAN PROJECT - DAY 5

  • Writer: MHK
    MHK
  • May 23, 2020
  • 2 min read

AUTHOR: MAHIR KAYA

DATE: MAY 23 / 2020

There are two functions left for us to implement before we go into the main program function, hangman_with_hints(secret_word).

Now, about our two functions, I called them hint functions because these functions will give hints to our user by showing him the possible words that matches with the guessed_word of user's.

Today, I am going to post our first function, match_with_gaps which takes two parameters, my_word and other_word.

my_word: This is the type of guessed_word, remember from our latest post? (ex: a_ p_ le)

other_word: This is a normal English word.

This function returns "TRUE" if other_word can be a possible match for my_word.

CODING PART:



#############HINT FUNCTION-1################
def match_with_gaps (my_word,other_word):
    list(my_word)
    # Converted str to list.
    p=0
    l1=[]
    my_word=my_word.replace(' ','')
    # Got rid of spaces that come after "_".
    if len(my_word)==len(other_word):
    # Checks if the length of two parameters are equal. 
        for x in my_word:
            if x !=('_'):
    # Makes sure that our character is not "_" before looking that if         
    #the character matches with the character at my_word. 
                if x==other_word[my_word.find(x,p)]:
                    l1.append('TRUE')
    # If characters match, appends "TRUE" to the emty list, l1.
                    if my_word.count(x)>0:
                        if my_word.count(x)!=other_word.count(x):
                            l1.append('FALSE')
    # In the game, if we guess a letter that is in the secret_word, 
    #game reveals all the places in secret_word that guessed letter is 
    #in. So, if a character is counted more than 0 in my_word and if 
    #character's count in the other_word do not match the count of the 
    #character in my_word, then that means the words do not match with 
    #each other so it add "FALSE" to l1. ( I may not've explained it 
    #very well, so ask question if you did not understand.)
                        
                else:
                    l1.append('FALSE')
    # If characters do not match, appends "TRUE" to the emty list, l1.
                  
            p+=1
    else:
        l1.append('FALSE')
    # If lengths of the words are not equal, adds "FALSE" to l1.
    length=len(l1)
    if length==l1.count('TRUE'):
        return('TRUE')
    # Checks if all the characters' of l1 is True. If it is, it returns 
    #"True".
    else:
        return("FALSE")
    # If there is just one "FALSE", it returns "FALSE".

コメント


Created By Root Team 

  • Facebook
  • Twitter
  • YouTube
  • Pinterest
  • Tumblr Social Icon
  • Instagram
bottom of page