Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7713993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:00:45+00:00 2026-06-01T02:00:45+00:00

I’m working on a Python version of hangman that works off of a .txt

  • 0

I’m working on a Python version of hangman that works off of a .txt file wordlist. For some reason, in playthroughs after the initial playthrough, the script won’t show certain letters. Everything else works the way I want it to, more or less.

My code:

import random

wordlist = open("wordlist.txt").read().split()
word = random.choice(wordlist)
strikes = 0
hidden_letters = []
alphabet = "abcdefghijklmnopqrstuvwxyz"
guesses = []

def new_word():
    global word
    global guesses
    word = random.choice(wordlist)
    for letter in set(word):
        if letter in alphabet:
            hidden_letters.append(letter)
    guesses = []

def current_progress():
    for letter in word:
        if letter in hidden_letters:
            print '_',

        elif letter == ' ':
            print ' ',

        else:
            print letter,
    print "\n"

def play_again():
    global strikes
    print "Would you like to play again?"
    answer = raw_input("y/n: ")
    if answer == "y":
        strikes = 0
        main()
    elif answer == "n": exit(0)
    else:
        print "That's not a valid answer."
        play_again()

def letter_in_word(x):
    global strikes
    global hidden_letters
    if x in word:
        hidden_letters.remove(x)
        print "That letter is in the word."
        current_progress()
        if hidden_letters == []:
            print "You win!"
            play_again()
        else:
            print "You have %d strike(s)." % strikes

    elif not x in word:
        print "That letter is not in the word."
        current_progress()
        strikes = strikes + 1
        print "You have %d strike(s)." % strikes

def main():
    new_word()
    current_progress()
    global strikes
    while strikes < 6 and not hidden_letters == []:
        print "Guess a letter. \n Letters that have been already guessed are:", guesses

        guess = raw_input("> ")

        if guess in alphabet and len(guess) == 1:
            if not guess in guesses:
                guesses.append(guess)
                letter_in_word(guess)

            else:
                print "You've already guessed that letter. Pick another."
                current_progress()
                print "You have %d strikes." % strikes

        else:
            print "Sorry, that's not a valid guess."
            current_progress()
            print "You have %d strikes." % strikes

    if strikes == 6:
        print "Oop! You lose."
        print "The answer was:", word
        play_again()

print "Welcome to Hangman!"
print "Six strikes and you lose."
print "----------"      
main()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:00:47+00:00Added an answer on June 1, 2026 at 2:00 am

    Your problem: when the player guesses a letter that exists in the current word and in a previous word, but was not guessed in the previous word (the player lost that game), the letter is still in the hidden_letters list because remove(x) only removes the first instance of that letter. In other words, your list contains two of the same letter during some executions, which violates an implied requirement of your code. You can fix this by adding hidden_letters = [] in your new_word() function before you add the new word’s letters.

    From the Python tutorial (emphasis mine):

    list.remove(x)

    Remove the first item from the list whose value is x. It is an error
    if there is no such item.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.