def findall(L, value, start=0): # generator version i = start try: i = L.index(value, i+1) yield i except ValueError: pass def buildBlankGuess(secretWord): guessed = '' for c in secretWord: guessed = guessed + '_' return guessed def buildGuessed(secretWord, found, index): g = buildBlankGuess(secretWord) newWord = list(g) newWord[index] = found return ''.join(n for n in newWord) def getGuessedWord(secretWord, lettersGuessed): for c in lettersGuessed: for index in findall(secretWord, c): word = buildGuessed(secretWord, c, index) return word print getGuessedWord('apple', ['c', 'p', 'l', 'l', 't', 'e']) test = '_ _ _ _'.replace(' ', '') t2 = list(test) print list(test) t2[0] = 'b' t2[3] = 'e' new = ''.join(n + ' ' for n in t2) print new
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question