Editing array – python

Question:

I’m trying to make Hangman game with arrays. But the arrays can’t be edited.
At first I was using the manual one without making a while loop like:

    z = 0
    r = "      "
    while z < len(word):
        r = r +grid[z]+" "
        z = z + 1

and I can easily print it like:

print("      "+grid[0]+" "+grid[1]+" "+grid[2]+" "+grid[3]+" "+grid[4]+" "+grid[5])

But with this code the word letters can’t be more and can’t be less than 6

I want the word letters can be more than 6 or less than 6 so it will be more flexible

Here is the full code

import random
import os

c = "Y"

def print_Hangman():
    print("☠️☠️☠️ | HANGMAN GAME | ☠️☠️☠️")
    print(r)
    print("      Stationary")
    if (chances == 6):
        print("   _____ n  |     | n  |     | n  |      n  |      n  |      n  |      n__|__n")
    elif (chances == 5):
        print("   _____ n  |     | n  |     | n  |     O n  |      n  |      n  |      n__|__n")
    elif (chances == 4):
        print("   _____ n  |     | n  |     | n  |     O n  |     | n  |      n  |      n__|__n")
    elif (chances == 3):
        print("   _____ n  |     | n  |     | n  |     O n  |     | n  |      n  |      n__|__n")
    elif (chances == 2):
        print("   _____ n  |     | n  |     | n  |     O n  |    /| n  |      n  |      n__|__n")
    elif (chances == 1):
        print("   _____ n  |     | n  |     | n  |     O n  |    /| n  |    /   n  |      n__|__n")
    elif (chances == 0):
        print("   _____ n  |     | n  |     | n  |     O n  |    /| n  |    /  n  |      n__|__n")
    global letters
    if (guessed_Letter > 0):
        if chances < 1:
            print("You Lose")
        else:
            letters = input("What letter do you think will be there: ")
    else:
        print("You Win")

while c == "Y":
    chances = 6
    word_List = ["pencil", "window", "rubber"]
    word = random.choice(word_List)
    guessed_Letter = len(word)

    n = len(word)
    grid = []
    while n > 0:
        grid.append("_")
        n = n - 1
    z = 0
    r = "      "
    while z < len(word):
        r = r +grid[z]+" "
        z = z + 1

    os.system('cls')

    while chances > 0 and guessed_Letter > 0:
        print_Hangman()
        while not len(letters) == 1:
            os.system('cls')
            print("Notif:nLetter can't be empty or more than one!")
            print_Hangman()

        if (letters in word):
            i = 0
            while i < len(word)+1:
                if (grid[i] == letters):
                    os.system('cls')
                    print("Notif:nYou have guessed it! ")
                    break
                if (word[i] == letters):
                    grid[i] = letters
                    guessed_Letter = guessed_Letter - 1
                
                i += 1

                if i == len(word):
                    os.system('cls')
                    break
        else:
            os.system('cls')
            chances = chances - 1
    print_Hangman()
    c = input("Play Again [Y/N]: ")

the "print(r)" doesn’t work it only prints "_ _ _ _ _ " when a letter is inserted
I expect it to be like "
_ _ _ I _"

Asked By: Nevyllo Kalangi

||

Answers:

A bit difficult to read the full code. Try to implement this example that i did, it does the job, i hope it can help you.

#Word example pencil, but works for any word 
word = "pencil"

empty_spaces = []
leng_word = len(word)

#Create the spaces according to len 
for i in range(0,leng_word):
    empty_spaces.append("_")

guess_letter = input("Enter a letter")

#n to take the idex, there are other ways also
n=0

#This loop searchs the coincidence and replaces the word if true
for letter in word:
    if guess_letter == letter:
        empty_spaces[n]=letter
    n+=1
print(empty_spaces)
Answered By: Matias Navarrete
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.