Search code examples
pythonpython-3.xfunctionprintingreturn

The output prints none and is not enclosed with ''


def isWordGuessed(secretWord, lettersGuessed):
    for char in secretWord:

        if char not in lettersGuessed:
            print('_',end=' ')
        else:
            print(char,end=' ')

print(isWordGuessed('apple', ['e', 'i', 'k', 'p', 'r', 's']))

The output is _pp_eNone

I want my output to be _pp_e while still using the print function to call the function.

What should I do?


Solution

  • You can simply modify this to:

    def isWordGuessed(secretWord, lettersGuessed):
         for char in secretWord:
             if char not in lettersGuessed:
                 print("_", end="")
             else:
                 print(char, end="")
         return ""
    
    print(isWordGuessed('apple', ['e', 'i', 'k', 'p', 'r', 's']))
    

    The None is the implicit return value after you have looped through your for loop.