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

Recommended Answers

All 7 Replies

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

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.

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()

Ok, thanks!I didn't know that!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.