1

I am writing a code for hangman game. In which I pick one random word from a text file and then user have to guess that word letter by letter. I face one problem while I iterating loop from the correct word. I try to iterate this loop for a comapring current word with the gussed word

I tried many solutions from other websites but it won't be working. I also tried run this code in idle line by line but with some different and logically both are same and In idle it won't give me any error or any warning

    def guess_word(letr,empty,correct_word):
        if(letr.upper() in empty):
            return("Already guessed!!" + empty)
        elif(letr.upper() in correct_word):
            print("You gussing correctly")
            n=0
            for n in correct_word:
                print(type(empty))
                if correct_word[n]==letr:
                    print(type(empty))
                    empty.replace(empty[n],letr.upper())
                    n+=1
            return(empty)

in main function:

while correct_word!=e:
        letr=input("Guess your letter: ")
        z=guess_word(letr,empty,correct_word)
        print((z))

This the function in which I face an error. I enter code here`ant to compare correct_word(actual word) letter by letter(letr) with empty(user's guessed word) and replace a letter with the empty string letter. And I also include some part of my main function which shows how I call my function

Hai Vu
  • 37,849
  • 11
  • 66
  • 93
Bhoomika
  • 11
  • 1

2 Answers2

0

I guess that 'correct_word' is in fact a string, so 'n' is a char. You can't use a char as an index, since indexes should be of int type. You need to use enumerate() to keep a count of iterations over the iterable object (correct_word):

for i, n in enumerate(correct_word):
    print(type(empty))
    if n == letr:
        print(type(empty))
        empty.replace(empty[i],letr.upper())

Furthermore, the following line of code seems to be "functionally" wrong if 'empty' is, indeed, an empty or pseudo empty (e.g. full of *) string; empty.replace(empty[i],letr.upper()). Take a look over https://www.geeksforgeeks.org/python-string-replace/ and Change one character in a string?.

danrodlor
  • 1,329
  • 8
  • 16
0

First of all, I have a few questions.

What is e in this line in the main file correct_word!=e

Possible solution steps:-

  • Pick a word from file
  • Loop over the word to create a dictionary of keys as character and value as list of all indexes at which that word appears (for example :- word can be "apple" thus charDictionary will look something like {"a": [0], "p": [1,2], "l": [3], "e": [4]}
  • Create a variable, lets call it "result" which can be a list of the same size as selected / correct word with all indexes initially filled with _ (underscore character) for example for the word apple result will look something like result=[_,_,_,_,_]
  • Create an list or tuple of characters from a to z.
  • Set number of lives (meaning the number of chances user gets to guess the word)
  • Loop over number of lives decreasing 1 at each iteration til it becomes zero.
    • Show Available words to user at each iteration.
    • Ask user to pick a character from the above list.
    • check if picked character is in the list if so then continue else do some appropriate action.
    • check if the word is a key in charDictionary, if so then loop over the value of that character in charDictionary and fill all indexes of "result" variable with that character (for example with apple we have something like [,,,,] in "result" variable and user picks p character and in charDictionary has index 1 and 2 in its result so now result variable will become [,p,p,,])
    • Find the index of _(underscore character) on the result variable. If no underscore is found this means that the word as been completed and the user has won, if not then user can pick next character till he has chances left.
vipulbhj
  • 710
  • 5
  • 21