How to make my hangman game can find more than 2 double letter?
I had a problem in my hangman program finding more than 2 double letter word.
EX:if i had aabc
or aaabc
in my guess wordlist, then i guess a
b
c
is won't tell me i get right. But if i had abc
in guess wordlist, and i guess a
b
c
, then i get right.
Here what's my code are:
while keep_playing:
wordlist=["butterfly","tree","circumstances","jinrikisha"]
word=choice(wordlist)
word_len=len(word)
guesses=word_len * ['_']
max_incorrect=7
alphabet="abcdefghijklmnopqrstuvxyz"
letters_tried=""
number_guesses=0
letters_correct=0
incorrect_guesses=0
print_game_rules(max_incorrect,word_len)
while (incorrect_guesses != max_incorrect) and (letters_correct != word_len):
clues()
letter=get_letter()
if len(letter)==1 and letter.isalpha():
if letters_tried.find(letter) != -1:
print "You already picked", letter
else:
letters_tried = letters_tried + letter
first_index=word.find(letter)
if first_index == -1:
incorrect_guesses= incorrect_guesses +1
print "The",letter,"is not the mystery word."
else:
print"The",letter,"is in the mystery word."
lette开发者_StackOverflow社区rs_correct=letters_correct+1
for i in range(word_len):
if letter == word[i]:
guesses[i] = letter
else:
print "Please guess a single letter in the alphabet."
See how my wordlist all had more than 2 double letter, and never tell me i guess right, even i guessed all letter. I know once the word len letter is more than 2 (as double letter)is won't get it right, but how can i fix it?
The end-condition is incorrect because letters_correct
is only incremented by one each time (even if the letter occurred multiple times).
I would change the [victory] end-condition to "when there are no _'s in guesses
" which means that all the _'s have been replaced which means... (also make sure to take the input phrase "hello world" [and similar] into account when generating guesses
, if it matters).
Happy homeworking.
Bonus points for making the program more "modular": a trivial change is to make the main loop (with victory condition test) call a method which prompts for a letter and then updates the display.
精彩评论