I have this code that is a hangman game. I use while(there are blanks in the word):
. Then I use a for
loop to transfer a list version of it that is workable with, then I use a string version of it that they can see.
How am I supposed to update the visible version with the non visible version without printing out a ridiculous amount of _s?
**Here is some code to refer to.**
```
# -*- coding: utf-8 -*-
import random
import string
WORDLIST_FILENAME = "words.txt"
def load_words():
"""
Returns a list of valid words. Words are strings of lowercase letters.
Depending on the size of the word list, this function may
take a while to finish.
"""
# inFile: file
inFile = open(WORDLIST_FILENAME, 'r')
# line: string
line = inFile.readline()
# wordlist: list of strings
wordlist = line.split()
return wordlist
wordlist = load_words()
word_to_guess = random.choice(wordlist)
# end of helper code. A word to guess has been stored in the string
# variable 'word_to_guess'. +9
def hangman(word_to_guess):
solvinglst = []
solvingstr = " "
alphabet = "abcdefghijklmnopqrstuvwxyz"
num_guesses = len(word_to_guess)
for i in range(len(word_to_guess)):
solvinglst += "_"
print("Welcome to Hangman!")
print("-------------------------------------------------------------")
while("_" in solvinglst):
for i in solvinglst:
if solvingstr != solvinglst:
solvinglst[i].copy(solvingstr)
print(solvingstr)
print("You have ",num_guesses,"guesses.")
letter = input("What letter do you want to guess?:")
if letter in word_to_guess:
for i in range(len(word_to_guess)):
for x in range(len(alphabet)):
if alphabet[x] == letter:
alphabet = alphabet.replacea(alphabet[x],"_")
if word_to_guess[i] == letter:
solvinglst[i] = letter
if letter not in word_to_guess:
num_guesses -= 1
if num_guesses < 1 :
print("You lose.")
solvinglst = word_to_guess
if "_" not in solvinglst:
print(solvinglst)
break
print(num_guesses)
print(alphabet)
hangman(word_to_guess)
I have code above that to return a random word for them to guess. As you can see I already have a idea of what to do, it doesn't work but I need a simple solution to this code.