Python word game issue -
so looking @ python tutorial on how create hangman recently. tried out had issues source code, i've been trying write similar code works well. i've run issue. basically, whenever guess last letter in 'secret' string, doesn't replace blank letter. want know why happening , how can fix it.
here's code
import random words = '''aardvark baboon calf camel deer dingo alligator ant jackal iguana falcon flamingo macaw manatee marmoset flee emu narwhal leopord ocelot lemming opossum dog dolphin dove beaver bison wallaby rabbit salamander seagull sheep skunk tiget tortoise unicorn dragon zombie'''.split() def randomword(wordlist): windex = random.randint(0, len(wordlist) - 1) return wordlist[windex] def displaygame(wrong, right, secret): print('wrong guesses:', end=' ') letter in wrong: print(letter, end=' ') print() blanks = '_' * len(secret) in range(0, len(secret) - 1): if secret[i] in right: blanks = blanks[:i] + secret[i] + blanks[i+1:] letter in blanks: print(letter, end=' ') print() def getguess(guessed): while true: guess = input() guess = guess.lower() if len(guess) != 1: print('please enter single letter.') elif guess in guessed: print('you have guessed letter.') elif guess not in 'abcdefghijklmnopqrstuvwxyz': print('please print letter.') else: return guess def playagain(): print('would play again? (yes or no)', end=' ') while true: keepgoing = input() if keepgoing == 'yes': return true elif keepgoing == 'no': return false else: print('yes or no please.') print('guessing game!') print() wrong = '' right = '' secret = randomword(words) gameover = false while true: while gameover == false: displaygame(wrong, right, secret) guess = getguess(wrong + right) if guess in secret: right += guess foundall = true in range(0, len(secret) - 1): if secret[i] not in right: foundall = false break if foundall: print('congratulations! won! word ' + secret) gameover = true break else: wrong += guess if len(wrong) > 7: print('you ran out of guesses! game over! word ' + secret) gameover = true break keepgoing = playagain() if keepgoing: wrong = '' right = '' secret = randomword(words) gameover = false else: print('thanks playing!') break
and here's broken output
would play again? (yes or no) yes wrong guesses: _ _ _ _ _ _ wrong guesses: _ _ _ _ _ m wrong guesses: m _ _ _ _ _ l wrong guesses: m l _ _ _ _ _ c wrong guesses: m l c _ _ _ _ _ e wrong guesses: m l c _ e _ e _ r wrong guesses: m l c _ e _ e _ d wrong guesses: m l c d _ e _ e _ b wrong guesses: m l c d b e _ e _ v congratulations! won! word beaver
the problem in
for in range(0, len(secret) - 1)
range default excludes last value, , subtracting 1
this
Comments
Post a Comment