0

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.

drewteriyaki
  • 320
  • 1
  • 3
  • 12
  • 1
    What is `>>for` and `>>if` supposed to mean? – Barmar Sep 28 '16 at 20:54
  • Why do you have two loops `for i in solvinglst:` and `for x in solvinglst:`? – Barmar Sep 28 '16 at 20:57
  • `if solvingstr != solvinglst:` will always succeed, since a string and a list are never equal to each other. – Barmar Sep 28 '16 at 20:57
  • that is what I have tried to do, but it keeps saying [list indices must be integers or slices, not str] – drewteriyaki Sep 28 '16 at 20:57
  • That's because of `solvinglst[x]`. `x` is the current character in the list, it makes no sense to use it as the index of the list. – Barmar Sep 28 '16 at 20:59
  • im not sure why thats there... must have been from my ppast code – drewteriyaki Sep 28 '16 at 20:59
  • I'm trying to copy everything from `solvinglst` to `solvingstr`. – drewteriyaki Sep 28 '16 at 21:01
  • I don't understand why you need two variables `solvinglst` and `solvingstr`. If you have a list of characters, you can create the corresponding string with `''.join(solvinglst)`. – Barmar Sep 28 '16 at 21:01
  • Let me put down all my code and it will make sense. The code isn't very long. – drewteriyaki Sep 28 '16 at 21:02
  • it is kind of unclear what your variables are for...solvinglst looks like its just a list of underscores the length of the word you're supposed to guess. but you're comparing it against solvingstr which is just a single space... (solvingstr = " ")...looking farther down in your code, it looks like you're just replacing the underscores with spaces. I think you may need to reapproach the problem and ask yourself what you really want to do....which for a first step is check if a certain character occurs in a string...then there are other steps after that, but right now you don't even have that. – Albert Rothman Sep 28 '16 at 21:04
  • I just reposted my full code that can play a full game of hangman, but all I need to to is make this `['_','_','_']` to this `_ _ _`. – drewteriyaki Sep 28 '16 at 21:33
  • Very different question than what you originally posted and also a duplicate of http://stackoverflow.com/questions/2906092/converting-a-list-to-a-string. It may not be immediately obvious, but if that's all you need to do, then you just need to convert the list to string before printing.. you might need to add the spaces manually before printing, which you can do easily with a list comprehension(which I would recommend looking up anyway, because they are very useful) – Albert Rothman Sep 28 '16 at 22:23

0 Answers0