Reputation: 33
When I use a word with 2 of the same letter (like 'snazzy') a guess only activates 1 letter at a time. How can I fix this?
l1=input('Input your (lowercase) letter:')
l2=input('Input your (lowercase) letter:')
l3=input('Input your (lowercase) letter:')
l4=input('Input your (lowercase) letter:')
l5=input('Input your (lowercase) letter:')
l6=input('Input your (lowercase) letter:')word=[l1,l2,l3,l4,l5,l6]
n1=''
n2=''
n3=''
n4=''
n5=''
n6=''
show=[n1,n2,n3,n4,n5,n6]
fail=0
good=0
while fail<=6 and good<6:
guess=input('Guess a letter...')
if guess in word:
print('Right!')
good=good+1
if guess==l1:
n1=guess
elif guess==l2:
n2=guess
elif guess==l3:
n3=guess
elif guess==l4:
n4=guess
elif guess==l5:
n5=guess
elif guess==l6:
n6=guess
show=[n1,n2,n3,n4,n5,n6]
else:
print('No.')
fail=fail+1
print(show)
print(word)
if fail==7:
print('Executioner wins!')
else:
print('Prisoner wins!')
To clarify, I cannot guess the letter twice to show all of its instances.
Upvotes: 1
Views: 28
Reputation: 16505
Well, there are many things in your code that are not optimal, but here is a small improvement (which is also not optimal). I used a for loop to look for all letters that match the guess.
num_letters = 6
word_to_guess = []
for _ in range(num_letters):
word_to_guess.append(
input('Input your (lowercase) letter:').lower().strip())
word_to_show = ['?', ] * num_letters
fail = 0
good = 0
while fail <= num_letters and good < num_letters:
guess = input('Guess a letter...').lower().strip()
if guess in word_to_guess:
print('Right!')
for i, letter in enumerate(word_to_guess):
if guess == letter:
good += 1
word_to_show[i] = letter
else:
print('No.')
fail += 1
print(word_to_show)
print('word_to_guess', word_to_guess)
print('word_to_show', word_to_show)
if fail == 7:
print('Executioner wins!')
else:
print('Prisoner wins!')
Does that work for you?
Upvotes: 1