Reputation: 169
I am trying to make a hangman game in python, it works but I want to make the UserGuesses list remember all 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 a letter (caps only): '))
for i 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 is the output:
C:\Users\owner\Desktop\P\HangmanPython>python hangman.py
Guess a letter (caps only): A
['_', '_', '_', '_', '_', '_', '_', '_']
Guess a letter (caps only): S
['_', '_', '_', '_', '_', '_', 'S', '_']
Guess a letter (caps only): W
['_', '_', '_', '_', '_', '_', '_', '_']
Guess a letter (caps only): R
['R', '_', '_', '_', '_', '_', '_', '_']
Guess a letter (caps only): G
['_', '_', '_', '_', '_', '_', '_', '_']
Guess a letter (caps only): Y
['_', '_', '_', '_', '_', '_', '_', '_']
Guess a letter (caps only): H
['_', '_', '_', '_', '_', '_', '_', '_']
Guess a letter (caps only): O
['_', '_', '_', '_', '_', '_', '_', '_']
Guess a letter (caps only): P
['_', '_', '_', '_', '_', '_', '_', '_']
Score: 2 / 8
The word was: RUMMIEST
I would like for each list to keep the previous output, for the third list and all the ones after that, to keep the 'S'.
Upvotes: 0
Views: 828
Reputation: 8982
And without recursion:
import random
HangmanWords = random.choice(open('words.txt').readlines())
word = str(HangmanWords)
w = word.strip()
NumOfTries = len(w)
def turns(NumOfTries, word):
score = 0
guesses = set()
for i in range(len(w)):
guess = str(raw_input('Guess a letter (caps only): '))
guesses.add(guess)
if guess in word:
score += 1
print [c if c in guesses else "_" for c in w]
return score
score = turns(NumOfTries, w)
print 'Score:', score, '/', NumOfTries
print 'The word was:', word
We don't need a list for the score.
Upvotes: 1
Reputation: 90929
The issue is that in each recursion of the turns
function , you are re-initializing the UserGuesses variable, instead you can try defining it outside the function and use it inside, I believe that is possible in python, but then you will also need to keep track of the index of letters in the word, and instead of doing UserGuesses.append
, you will need to change the index value, something like -
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 a letter (caps only): '))
j = 0
for i 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 = ['_' for i in w]
turns(NumOfTries,w,score)
S = len(score)
print 'Score:',S,'/',NumOfTries
print 'The word was:', word
Upvotes: 1