0

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

4
Contributors
7
Replies
20
Views
4 Years
Discussion Span
Last Post by glao
0
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 by flebber: better solution

0

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

0

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'.

0

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

1

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 by woooee

1

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.