Issue
So I've got this list: visual = ['_ ', '_ ', '_ '] And the user enters a random letter to find the end word (the game is hangman) I have a way of printing it out but it looks like this:
_
o
_
Whereas I want it to look like this: _ o _ Or: ['_ ', 'o ', '_ '] I don't mind which one prefer first one.
I have this so far:
visuals = ['_ ', '_ ', '_ ']
userInput = input("Enter a letter to guess: ")
index = 0
for ch in hangmanWord:
index += 1
if ch == userInput:
visuals = [w.replace([index], userInput) for w in visuals]
print(visuals)
But the last line doesn't seem to work(I think it's the replacing line)it just comes out with this:
['_ ', '_ ', '_ ']
I want the underscores to be replaced by the users input.
Sorry for its length. Help is greatly appreciated.
Solution
This script solve the problem
>>> hangmanWord = "hello"
>>> visuals = ['_'] *len(hangmanWord)
>>> ended = False
>>> while not ended:
... userInput = raw_input("Enter a letter to guess: ")
... for idx,ch in enumerate(hangmanWord):
... if userInput == hangmanWord[idx]:
... visuals[idx] = userInput
... print visuals
... ended = '_' not in visuals
...
Enter a letter to guess: a
['_', '_', '_', '_', '_']
Enter a letter to guess: h
['h', '_', '_', '_', '_']
Enter a letter to guess: e
['h', 'e', '_', '_', '_']
Enter a letter to guess: l
['h', 'e', 'l', 'l', '_']
Enter a letter to guess: o
['h', 'e', 'l', 'l', 'o']
Answered By - xecgr
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.