python - How do I make the output in this hangman game keep its previouse outputs? -


i trying make hangman game in python, works want make userguesses list remember inputs.

import random  hangmanwords = random.choice(open('words.txt').readlines()) word = str(hangmanwords) w = word.strip() #tries = number letters numoftries = len(w)  score = [] def turns(numoftries,w,score):   userguesses = []   ui = str(raw_input('guess letter (caps only): '))    in w:     if ui == i:       score.append('yes')       userguesses.append(ui)     else:       userguesses.append('_')    print userguesses    while numoftries > 0:     turns(numoftries - 1,w,score)     break  turns(numoftries,w,score)  s = len(score) print 'score:',s,'/',numoftries print 'the word was:', word 

this output:

c:\users\owner\desktop\p\hangmanpython>python hangman.py guess letter (caps only): ['_', '_', '_', '_', '_', '_', '_', '_'] guess letter (caps only): s ['_', '_', '_', '_', '_', '_', 's', '_'] guess letter (caps only): w ['_', '_', '_', '_', '_', '_', '_', '_'] guess letter (caps only): r ['r', '_', '_', '_', '_', '_', '_', '_'] guess letter (caps only): g ['_', '_', '_', '_', '_', '_', '_', '_'] guess letter (caps only): y ['_', '_', '_', '_', '_', '_', '_', '_'] guess letter (caps only): h ['_', '_', '_', '_', '_', '_', '_', '_'] guess letter (caps only): o ['_', '_', '_', '_', '_', '_', '_', '_'] guess letter (caps only): p ['_', '_', '_', '_', '_', '_', '_', '_'] score: 2 / 8 word was: rummiest 

i each list keep previous output, third list , ones after that, keep 's'.

the issue in each recursion of turns function , re-initializing userguesses variable, instead can try defining outside function , use inside, believe possible in python, need keep track of index of letters in word, , instead of doing userguesses.append , need change index value, -

import random  hangmanwords = random.choice(open('words.txt').readlines()) word = str(hangmanwords) w = word.strip() #tries = number letters numoftries = len(w)  score = [] def turns(numoftries,w,score):   ui = str(raw_input('guess letter (caps only): '))    j = 0   in w:     if ui == i:       score.append('yes')       userguesses[j] = ui     j = j + 1    print userguesses    while numoftries > 0:     turns(numoftries - 1,w,score)     break   userguesses = ['_' in w] turns(numoftries,w,score)  s = len(score) print 'score:',s,'/',numoftries print 'the word was:', word 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

node.js - Using Node without global install -

php - CakePHP HttpSockets send array of paramms -