Reputation: 55
How can I store the users input from the hangman function, so every time the for loop runs it remembers the user has already guessed the letter.
When the hangman function runs the first time, if the user's input is 'gera', that's fine, it should then loop back for a fifth guess; the problem is if the user enters 'mo' now, it won't show like this gera _ _ mo, instead it'll print _ _ _ _ _ _ mo; forgetting the users previous 'gera' input. How can I store past inputs so the code recalls what was passed?
def get_guessed_word(secret_word, letters_guessed):
a = ""
for char1 in secret_word:
if char1 in letters_guessed:
a += char1
else:
a += "_ "
print(a)
def get_available_letters(letters_guessed):
a = string.ascii_lowercase
for char1 in a:
if char1 in letters_guessed:
a = a.replace(char1,"")
print(a)
def hangman(secret_word_user_to_guess):
secret_word_user_to_guess = 'geranimo'
for index in range(6,1,-1):
print('You have', index,'guesses left.')
#print('Available letters: abcdefghijklmnopqrstuvwxyz')
letters_guessed = input('Check a word: ')
get_guessed_word(secret_word_user_to_guess, letters_guessed)
print('Available letters:')
get_available_letters(letters_guessed)
Upvotes: 0
Views: 246
Reputation: 15310
Let's look at your function, because there's a concept you don't appear to have:
def g(secret_word, letters_guessed):
a = []
for char1 in secret_word:
for thing in letters_guessed:
if thing == char1:
a += thing
break
elif thing != char1:
a[secret_word.index(char1)] == '_ '
break
The line for char1 in secret_word:
iterates over the word, pulling each character into char
in turn. There's nothing wrong with this, and pretty much every solution will have to use it.
Next, you do for thing in letters_guessed:
. There's a problem with this, as pointed out by @ForceBru, in that Python supplies the in
and not in
operators to do what you're trying to code by hand.
However, when you're just starting out it's okay to re-invent the wheel a few times. If nothing else, it helps you understand what's happening under the hood.
The thing I think you've missed, or misunderstood, is this: when you have char1 == thing
you know something. You can absolutely state that "yes, this letter has been guessed" and you can proceed to show the letter.
But when that condition isn't true - when char1 != thing
- you don't know anything. You can't simply say "oh, if that isn't true then draw an underscore" because you aren't finished checking!
Remember that the for thing in letters_guessed:
loop is checking each of the letters that have been guessed. This means that even though this particular character in letters_guessed might not match the current character in the word, it's possible that some other character will match, so you have to keep looping.
Something like this:
for char1 in secret_word:
for thing in letters_guessed:
if thing == char1:
a += thing
break
else: # 'else' after for means 'if no break command was run'
a += '_'
Upvotes: 0
Reputation: 607
There are several ways to solve it like ForceBru already mentioned. But to add one with sets as proposed in the comments:
def g(secret_word, letters_guessed):
letters = set(secret_word)
guess = set(letters_guessed)
for i in letters.difference(guess):
secret_word=secret_word.replace(i,'_')
print(secret_word)
secret_word = 'apple'
letters_guessed = ['e', 'i', 'k', 'p', 'r', 's']
print(g(secret_word, letters_guessed) ) #prints '_pp_e'
Upvotes: 0
Reputation: 44858
You can do this easily:
result = ""
for char in secret_word:
if char in letters_guessed:
result += char
else:
result += "_"
Or, using a generator:
result = "".join(char if char in letters_guessed else "_" for char in secret_word)
BTW, letters_guessed
could be a string ("eikprs"
) or, if the order doesn't matter, a set
: set("eikprs")
.
You can also try this:
result = secret_word.translate("".maketrans(letters_guessed, "_" * len(letters_guessed)))
Upvotes: 0