Reputation: 79
I'm trying to make a text based hangman game. I have two lists:
I can check for the words in the first list and replace it, only if there is one occurrence.
For example: the word is 'DEAD' and the user inputs D. The output would then be D*** because it only finds one.
Current code to replace from one list to another list:
if guess() == True:
pos = word.index(guessed)
display[pos] = word[pos]
print('Correct: ', display)
How can i change the code so it replaces every occurrence in the list?
Upvotes: 0
Views: 262
Reputation: 180461
word = "DEAD"
inp = "D"
print("".join([s if inp == s else "_" for s in word]))
D__D
You should also use if guess()
if guess() == True
, if guess() will either be True or False if it retutns a boolean.
If you are using functions:
secret_word = "DEAD"
def is_guessed(secret_word, letters_guessed):
return all(x in letters_guessed for x in secret_word)
def get_guessed_word(secret_word, guessed):
return "".join([letter if letter in guessed else "_" for letter in secret_word])
letters_guessed = set()
guess = ""
lives = 5
while True:
if is_guessed(secret_word,letters_guessed):
print("Congratulations you guessed {}.".format(secret_word))
break
elif lives == 0:
print("Out of lives the word was {}.".format(secret_word))
break
print("You have {} lives remaining".format(lives))
inp = input("Enter a letter ").upper()
if inp in letters_guessed:
print("Already picked that letter")
continue
letters_guessed.add(inp)
if inp.lower() in secret_word:
print("Good guess")
print(get_guessed_word(secret_word, letters_guessed))
else:
print("{} is not in the word".format(inp))
lives -= 1
Upvotes: 0
Reputation: 3587
word = "dead"
right_answer = set() #1
def replace_star(): # this part is relevant to your quesiton
uncoverd = "" # first it makes a empty string
for char in word: # loops trough every character in the word
if char in right_answer: # if the character is in the set of right_answers
uncoverd += char # it adds the character to the empty string
else: # if is not
uncoverd += "*" # it add a '*'
return uncoverd # returns the new word with all the character replaced with eiter a '*' or the guesses right character
score = 10
while True:
uncoverd = replace_star()
print ("Word:",uncoverd)
answer = input("Guess one character of the word.\n>>")
if answer in word: # 2
print ("Spot on!")
right_answer.add(answer) # 3
if right_answer == set(word): # 4
print ("You Win!")
print ("The word was:", word)
break
else:
score -= 1
print ("Wrong")
print ("Score: '%s'"%score)
if score == 0:
print ("You Lose!")
break
By using a set you can check if all the unique items of a sequence are present in another
word
right_answer
setright_answer
is equal a the set of characters in the word.Upvotes: 0
Reputation: 117926
You can use zip
to compare the answer to the currently completed string letter-by-letter. Then use a generator expression within join
to check if the letter was correct, otherwise don't change it. You'll still need to add logic to keep track of how hanged they are.
answer = 'dictionary'
current = '*'*len(answer) # Produces '**********'
while current != answer:
guess = input('guess a letter: ')
current = ''.join(guess if guess == letter else blank for blank, letter in zip(current, answer))
print(current, '\n')
Testing
guess a letter: i
*i**i*****
guess a letter: d
di**i*****
guess a letter: c
dic*i*****
guess a letter: t
dicti*****
guess a letter: n
dicti*n***
guess a letter: l # Note this letter was wrong so the word didn't change
dicti*n***
guess a letter: o
diction***
guess a letter: r
diction*r*
guess a letter: a
dictionar*
guess a letter: y
dictionary
Upvotes: 4