0

I'm working on a mini project, it's a hangman game in python. I'm currently able to pick a randomwork from a list, ask user to guess and replace blanks with words. However, i am facing two challenges

  1. When blanks are replaced with correct letters, and the code runs again, the already placed alphabets are replaced with blanks are removed and the process begins again.
  2. I tried using the insert method, but i kept getting "AttributeError: 'NoneType' object has no attribute 'insert'"
import random

#Create a list holding the words
word_list = ["ardvak","donkey", "baboon", "elephant", "Tiger"]

#randomly chose a word from word_list and assign it to a variable called chosen_word
def randomWord():
    for word in word_list:
        global chosen_word 
        chosen_word = random.choice(word_list)
    print(f"random word :" +chosen_word)
randomWord()

#ask the user to guess a letter and assign that to a variable called guess. convert to lower case
def askUser():
    global guess 
    guess = input("Guess a letter: ").lower()
askUser()

#Check if guess is in chosen_word

# lenght of choosen word
def len_chosen_word1():
    global len_chosen_word
    len_chosen_word = len(chosen_word)
    
def guessWord():
    len_chosen_word1()
    guesses = 0
    alpha_count = 0
    global blanks
    blanks = []
    if guesses  < len_chosen_word:
        for alpha in chosen_word:
            alpha = alpha
            alpha_count+=1
            alpha_count-=1
            if guess in alpha:
               blanks = blanks.insert(alpha_count, guess) 
            else:
                blanks = blanks.insert(alpha_count,'_')
            guesses+=1
    print(blanks)
guessWord()

#search for occurence of blanks

while '_' in blanks:
    askUser();
    guessWord();

I tried using the insert method on the blank list and assigning it to a veriable to stop alphabets from disappearing

def guessWord():
    len_chosen_word1()
    guesses = 0
    alpha_count = 0
    global blanks
    blanks = []
    if guesses  < len_chosen_word:
        for alpha in chosen_word:
            alpha = alpha
            alpha_count+=1
            alpha_count-=1
            if guess in alpha:
               blanks = blanks.insert(alpha_count, guess) 
            else:
                blanks = blanks.insert(alpha_count,'_')
            guesses+=1
    print(blanks)
  • 1
    Remove the `blanks = ` from all `blanks = blanks.insert(...)` – luk2302 Nov 02 '22 at 10:16
  • 1
    Does this answer your question? [Python's insert returning None?](https://stackoverflow.com/questions/1516889/pythons-insert-returning-none) – luk2302 Nov 02 '22 at 10:17

1 Answers1

1

You don't need to assign the result of blanks.insert() to a variable

Replace

if guess in alpha:
    blanks = blanks.insert(alpha_count, guess) 
else:
    blanks = blanks.insert(alpha_count,'_')

To

if guess in alpha:
    blanks.insert(alpha_count, guess) 
else:
    blanks.insert(alpha_count,'_')

This is because the insert() is editing an existing list rather than creating a new one.

Also don't forget When did you announce the list x = [] the following will refer to an existing list rather than creating a new one.

x = ["a"]
y = x
y.append("b")

print(x)
# ["a", "b"]
Devid Mercer
  • 160
  • 1
  • 2
  • 8
  • Thank you, I tried this; the code runs but not as expected. For example, let's say the random word is ANT and the user guesses "N" correctly, so the output becomes "_ N _." The code should run until no blanks are visible, so when the user guesses again, let's say "T," the output should be "_ N T," but my output is "_ _ T." It's not storing the correct guesses. – Abdulhameed Yunusa Nov 03 '22 at 16:25