Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hangman
#1
def split(word): 
    return list(word)

def cw():
    n = 0
    print("Welcome to Hangman !")
    print("You have 8 guesses left")
    Alphabets = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
    TAlphabets = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']*5
    print("Available letters are:",Alphabets)
    word = input("Please store word: ")
    lettersinword = split(word)
    print(lettersinword)
    print("The word in list format is", lettersinword)
    print(type(lettersinword))
    wordlength = len(lettersinword)
    print("Length of the word is: ",wordlength)
    print("Length of word as alphabets in list is: ",wordlength)
    InitialEmptyList = []*wordlength
    print(type(InitialEmptyList))
    print("The length of the new list is: ", len(InitialEmptyList))
    print("This is the list where the guessed letters will be filled in :",len(InitialEmptyList))
    Failedattempts =  8
    ConInitialEmptyList=''
    print("Number of failed attempts allowed is :",Failedattempts)
    while n < Failedattempts and ConInitialEmptyList!=word:
        letter = input("Please guess the letter :")
        if letter not in (lettersinword):
            print("Letter not in word :", letter)
            n = n+1
            print("You have exhausted",n,"out of",Failedattempts,"attempts")
        else:
            print("Letter in word :", letter)
            x = lettersinword.index(letter)
            print("Position of the provided letter is :",x)
            TAlphabets.remove(letter)
            print("Available letters are: " ,TAlphabets)
            InitialEmptyList.insert(x,letter)
            print("You have guessed word:",InitialEmptyList)
            ConInitialEmptyList = ''.join(InitialEmptyList)
            print(type(ConInitialEmptyList))
            print(ConInitialEmptyList)
            print("The word you have guessed is :",ConInitialEmptyList)


I am trying build an easy version for hangman.I had to make 5 lists of alphabets to make
sure I have enough alphabets
for words having repeating letters.
Once I guess a letter, I should be able to identify all indexes of same letter and
insert all the repeatitions of the letter in InitialEmptyList as above in a single guess.
How do I do this.
Thanks
Reply
#2
When you start InitialEmptyList, an empty list times 5 is just an empty list. I would do something like ['-'] * len(word), so you can later assign by index.

I'm not sure why you have lettersinword. The 'in' operator and the index method both work fine on strings, you don't need to convert the word to a list to use them.

Put those two points together, and when you find a word it can be assgined with InitialEmptyList[word.index(letter)] = letter. That part should be in a loop though, so you can find multiple instances of the letter. Note that index takes a start parameter, which tells it where in the sequence to start looking. The loop would look like:

start = 0
while letter in word[start:]:
    location = word.index(letter, start)
    InitialEmptyList[location] = letter
    start = letter + 1
Your variable names are confusing. For one thing, some are all lower case, and others are camel case. Pick one and stick with it. While it's good that you are using descriptive variable names, the descriptions are confusing. Frex, InitialEmptyList is the guessed word so far, so I would call it guessed_word, or even guess.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
This is what I ended up with and it works good.
Please advise more efficient ways.

Appreciate the responses.

def cw():
n = 0
print("Welcome to Hangman !")
print("You have 8 guesses left")
word = input("Please store word: ")
lettersinword = split(word)
print("The letters in the word are: ",lettersinword)
wordlength=len(lettersinword)
InitialEmptyList = ['']*wordlength
print("The length of the new list is :",len(InitialEmptyList))
print(InitialEmptyList)
Failedattempts = 8
ConInitialEmptyList=''
print("Number of failed attempts allowed is :",Failedattempts)
while n < Failedattempts and ConInitialEmptyList!=word:
letter = input("Please guess the letter :")
if letter not in (lettersinword):
print("Letter not in word :", letter)
n = n+1
print("You have exhausted",n,"out of",Failedattempts,"attempts")
else:
print("Letter in word :", letter)
x = lettersinword.index(letter)
print("Position of the provided letter is :",x)
lettersinword[x]=''
print("Available letters in the word are :",lettersinword)
InitialEmptyList[x]=letter
print("Filled letters in InitialEmptyList :",InitialEmptyList)
ConInitialEmptyList = ''.join(InitialEmptyList)
print(type(ConInitialEmptyList))
print(ConInitialEmptyList)
print("The word you have guessed is :",ConInitialEmptyList)
Reply
#4
You have been repeatedly advised to use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Sep-18-2019, 09:48 AM)metro17 Wrote: This is what I ended up with and it works good.
Please advise more efficient ways.
Appreciate the responses.

You say that it works good but i have doubts. Starting from line lettersinword = split(word). What is 'split'?

I concur with buran - if you are looking for help you should put your code into appropriate tags.

If you expect responses then people have to read your code. Therefore you should follow conventions set in PEP8 >>> Function and Variable Names:

Quote:Function names should be lowercase, with words separated by underscores as necessary to improve readability.

Variable names follow the same convention as function names.

mixedCase is allowed only in contexts where that's already the prevailing style (e.g. threading.py), to retain backwards compatibility.

I personally dislike 'ConInitialEmptyLis'. I don't use things that I dislike and in current context it means that I am less inclined to help than otherwise (but it just me).

However, I hope that you post runnable code in correct tags for review. I promise to give my feedback.

Meanwhile I can give you some ideas how to avoid long typing while creating list of alphabet characters (which was in your initial code).

(1) Save strokes on quotes by splitting string:

>>> 'a b c d e'.split()
['a', 'b', 'c', 'd', 'e']


(2) Save strokes by using codepoints:

>>> [chr(i) for i in range(97, 123)]
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trouble coding hangman Tay 1 2,370 Mar-28-2019, 01:57 AM
Last Post: ichabod801
  Hangman code problem KrakowKid 1 2,407 Feb-25-2019, 06:29 PM
Last Post: ichabod801
  Python hangman help A1395 11 7,179 Feb-13-2019, 04:24 PM
Last Post: ichabod801
  Hangman 2skywalkers 3 71,355 Oct-19-2018, 01:49 PM
Last Post: ichabod801
  Hangman Help. 2skywalkers 4 4,203 Jun-26-2018, 02:49 AM
Last Post: ichabod801
  Python Hangman Replacing "_" with letters. 2skywalkers 6 12,091 Jun-25-2018, 12:01 PM
Last Post: gruntfutuk
  Simple Hangman Game Issue andrew95 2 4,356 Apr-02-2018, 02:24 PM
Last Post: andrew95
  Designing Hangman game spacetimeguy 2 5,141 Feb-02-2018, 08:55 PM
Last Post: spacetimeguy
  TKinter Hangman Game Bumble 1 20,565 Jul-19-2017, 06:56 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020