Function not behaving same when called second time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jkthappeta
    New Member
    • May 2022
    • 1

    Function not behaving same when called second time

    In my guessing game, when the correct (five-letter) word is entered on the first try, it works fine (which means, it returns the correctly guessed word).

    But, when the correct word is entered on any other than the first try, it is returning None instead.

    Code:
    import random
    
    
    def word_list():
        with open("MyFile.txt", "r") as file:
            allText = file.read()
            return list(map(str, allText.split()))
        
    def random_word():
        rnd_word = (random.choice(word_list()))
        return rnd_word
    
    def is_real_word(guess, valid_words_list):
        if guess in valid_words_list:
            #check
            print("Passed word from next guess: ",guess)
            return True        
        else:
            #check
            print("Passed word from next guess: ",guess)
            print("That's not a real word!")
            next_guess(valid_words_list)
    
    def next_guess(word_list):       
        ges_word = input("Please enter a guess: ")
        
        if is_real_word(ges_word, word_list):
            #check
            print("next guess true ges word: ",ges_word)
            return ges_word 
         
    def check_guess(s1, s2):
        s = ''
        valid_list = list(s2)
            
        for i in range(len(s1)):
            if s1[i] in valid_list:
                if s1[i] in s2:
                    if i == s2.index(s1[i]):
                        s+="x"
                        valid_list.remove(s1[i])                    
                    else:
                        s+="o"
                        valid_list.remove(s1[i])                  
            else:
                s+="_"            
        return s
    
            
            
    def play(): 
                
        guessed_word = next_guess(word_list())
        #check
        print("Passed from next guess: ",guessed_word)
        correct_word = random_word()
        outcome = check_guess(guessed_word, correct_word)
       
        print("Random word: ", correct_word)
        print(outcome)
        
    play()
    Behavior when the correct letter is entered on the first try:
    Please enter a guess: kumar
    Passed word from next guess: kumar
    next guess true ges word: kumar
    Passed from next guess: kumar
    Random word: taunt
    _o_o_

    Behavior when the correct word is entered on the second try:
    Please enter a guess: aaaaa
    Passed word from next guess: aaaaa
    That's not a real word!
    Please enter a guess: kumar
    Passed word from next guess: kumar
    next guess true ges word: kumar
    Passed from next guess: None
Working...