-1
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?

Anuj
  • 994
  • 11
  • 21
  • 1
    Possible duplicate of [Python hangman, replacing letters?](https://stackoverflow.com/questions/26937153/python-hangman-replacing-letters) – cs95 Oct 30 '17 at 07:12
  • Sorry about all the confusion.I have literally joined this site 30 seconds ago. – divyanshu kumar Oct 30 '17 at 07:24
  • 1
    Ok I fixed it for you. When I run the code as it is now, I get `_ p p _ e`. Were you expecting something different? – Tom Karzes Oct 30 '17 at 07:29
  • How did you do this. Also I wanted to know if you can get '_pp_e' while using print(isWordGuessed('apple', ['e', 'i', 'k', 'p', 'r', 's'])) – divyanshu kumar Oct 30 '17 at 07:40

2 Answers2

0

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.

  • Thank you.But is there any other way to go about this problem. – divyanshu kumar Oct 30 '17 at 07:54
  • Yes, of course. You could just add a `return ""` after you loop. This would solve the problem that `None` gets returned after you have finished your for-loop. –  Oct 30 '17 at 08:16
0

Since your function isWordGuessed doesnt have any return keyword, the statement

isWordGuessed('apple', ['e', 'i', 'k', 'p', 'r', 's']) will return None.

Now that is called inside a print method, so None will be returned to print function and it will be printed.

The reason of it occurring at the end of the secretWord is due to usage of end parameter in your print statement inside the isWordGuessed

P.S. In python, variable names should be lowercase letters separated by underscores. Refer this

Anuj
  • 994
  • 11
  • 21