Reputation: 690
I am working on a hangman script but it isn't working properly. In this example I use the word "curiosities" as the word to guess, since it has three "i"s. F(or some reason styling is weird so in block):
The desired output is "_ _ _ i _ _ i _ i _ _"
But I can only get to "_ _ _ i _ _ i _ _ _ _"
here's the main part of the code:
def alter(output_word, guessed_letter, the_word):
checkword = the_word
#print ("outputword:",output_word)
previous = 0
fully_checked = False
current_count = 0
while fully_checked == False:
global add_number
checkword = checkword[previous:]
add_number = previous + add_number
#print ("..",checkword)
if guessed_letter in checkword:
current_count = current_count + 1
print (">>>",current_count)
the_index = (checkword.index(guessed_letter))+add_number
# print (output_word)
# print (the_index)
# print (guessed_letter)
output_word= output_word[:the_index-1] + guessed_letter + output_word[the_index:]
previous = the_index+1
else:
fully_checked = True
checkword = the_word
add_number = 0
print (' '.join(output_word))
checklist(current_count)
def checklist(current_count):
print ("checklist")
print (current_count)
global total_count
total_count = total_count + current_count
print ("total count is", total_count)
Somewhere else in the code to trigger this all(the_word being curiosities and guessed_letter being i):
if guessed_letter in the_word:
alter(output_word, guessed_letter, the_word)
Upvotes: 1
Views: 69
Reputation: 55469
As Derorrist points out, there are a couple of flaws in your code's logic, the main one being you've mixed up the index of output_word
with that of checkword
.
I've fixed those bugs and condensed your code a little. add_number
doesn't need to be a global, and in general you should avoid using modifiable globals unless you really need them, because they destroy code modularity.
total_count = 0
def alter(output_word, guessed_letter, checkword):
previous = add_number = current_count = 0
while True:
checkword = checkword[previous:]
if guessed_letter in checkword:
the_index = checkword.index(guessed_letter)
offset = the_index + add_number
print(">>>", current_count, the_index, offset)
output_word= output_word[:offset] + guessed_letter + output_word[offset + 1:]
previous = the_index + 1
add_number += previous
current_count += 1
else:
break
print(' '.join(output_word))
checklist(current_count)
return output_word
def checklist(current_count):
global total_count
print("checklist")
print(current_count)
total_count += current_count
print("total count is", total_count, end='\n\n')
# test
the_word = "curiosities"
output_word = "_" * len(the_word)
print(' '.join(output_word))
for guessed_letter in 'cisotrue':
output_word = alter(output_word, guessed_letter, the_word)
output
_ _ _ _ _ _ _ _ _ _ _
>>> 0 0 0
c _ _ _ _ _ _ _ _ _ _
checklist
1
total count is 1
>>> 0 3 3
>>> 1 2 6
>>> 2 1 8
c _ _ i _ _ i _ i _ _
checklist
3
total count is 4
>>> 0 5 5
>>> 1 4 10
c _ _ i _ s i _ i _ s
checklist
2
total count is 6
>>> 0 4 4
c _ _ i o s i _ i _ s
checklist
1
total count is 7
>>> 0 7 7
c _ _ i o s i t i _ s
checklist
1
total count is 8
>>> 0 2 2
c _ r i o s i t i _ s
checklist
1
total count is 9
>>> 0 1 1
c u r i o s i t i _ s
checklist
1
total count is 10
>>> 0 9 9
c u r i o s i t i e s
checklist
1
total count is 11
FWIW, there are simpler ways to do this that are also more efficient. Eg, here are a couple I wrote the other day.
Upvotes: 1
Reputation: 2993
You mixed the index of output_word
with the index of the sliced checkword
.
Replace previous = the_index+1
with
previous = checkword.index(guessed_letter)+1
Also there's a small slicing mishap in output_word= output_word[:the_index-1] + guessed_letter + output_word[the_index:]
It should be
output_word= output_word[:the_index] + guessed_letter + output_word[the_index+1:]
Output:
_ _ _ i _ _ i _ i _ _
Upvotes: 1