Hello , I am trying to implement this function:

def getGuessedWord(theword, wordlist):


    for letter in theword:
        if letter in wordlist:
            print  letter,         

        else:
            print  '_',

For example:

theword = 'hi'
wordlist = ['e', 'i', 'o', 's']

must return : _i

but it returns : _iNone

I tried :

  for letter in secretWord:
        if letter in lettersGuessed:
            print  letter,         

        else:
            print  '_',
    return letter

but it returns : _ii

theword = 'hi'
wordlist = ['e', 'i', 'o', 's']


def word(n):
    result = []
    for i in tuple(theword):
        if i in wordlist:
            result.append(i)
        else:
            result.append('_')
    a = ''.join(result)
    return a

print(word(wordlist))

From that I get

sayth@linux-g8u9:~/dev> python guess.py 
_i

Edited 3 Years Ago by flebber: better solution

Thanks for the answer ,but if I don't want to use a list?
Is there a way?

I upadted the answer to convert back to a string.

you need to break up 'hi' to 'h','i' when searching wordlist as 'hi' isn't an element and thats why you get 'none'.

Ok , thanks , but there is no other way with just using 'print' statement?Without using list or tuple at all?

but it returns : _iNone

The "None" is returned by the function so apparently you are calling it with "print function_name()" instead of "function_name". If that does not help then post the entire code including how you call it.

Edited 3 Years Ago by woooee

The better practise generally is to return value and print the value, not print inside function. So if you have choise keep the call and make the function to return the value otherwise remove the print from calling it, just do not forget the parenthesis after name

function_name()
This question has already been answered. Start a new discussion instead.