Eelco
Eelco

Reputation: 49

Hangman prints word multiple times if a letter occurs multiple times

I am building a Hangman game. The code that I wrote prints the 'updated target word' multiple times if a letter occurs multiple times.

Example: the target word is 'celebrate'. If I guess e then it prints

*e******
*e*e****
*e*e***e

I would like to avoid printing the first two printouts and only print the third and most updated version.

import random
import re

word_list = ["fireboard", "identical", "chocolate", "christmas", "beautiful", "happiness", "wednesday", "challenge", "celebrate"]

random_pick = random.choice(word_list)
random_pick_a = re.sub("[a-z]","*", random_pick)
random_pick_list_a = list(random_pick_a)
print(random_pick)
count = 0


def main_function():
    global count
    while count <= 9:
        user_input = str(input("type a letter:"))
        for i, c in enumerate(random_pick):
            if c == user_input.casefold():
                random_pick_list_a[i] = user_input.casefold()
                random_pick_list_b = ''.join(random_pick_list_a)
                print(random_pick_list_b)
                if random_pick_list_b == random_pick:
                    print("done")
                    exit()
                else:
                    continue
        else:
            if user_input.casefold() not in random_pick:
                count = count+1
                print(count)
                if count == 10:
                    print("sorry")
                    exit()


main_function()

Disclaimer: I am in my first weeks of coding!

Upvotes: 3

Views: 64

Answers (2)

Psytho
Psytho

Reputation: 3384

No need to str() the input(), it's already a string. So strip str(input("type a letter:")) to input("type a letter:").

No need in

else:
    continue

it will continue even without it. Don't use globals, just move your count into main_function().

Don't do if count == 10, you're already doing it in while count <= 9.

As for your question - move the block

print(random_pick_list_b)
if random_pick_list_b == random_pick:
    print("done")
    exit()

out of the for-loop. So the whole thing would look like this:

def main_function():
    count = 0
    while count <= 4:
        user_input = input("type a letter:")
        for i, c in enumerate(random_pick):
            if c == user_input.casefold():
                random_pick_list_a[i] = user_input.casefold()
                random_pick_list_b = ''.join(random_pick_list_a)
        print(random_pick_list_b)
        if random_pick_list_b == random_pick:
            print("done")
            exit()
        else:
            if user_input.casefold() not in random_pick:
                count = count+1
                print(count)
    print("sorry")

Upvotes: 1

Lou Franco
Lou Franco

Reputation: 89172

You have:

print(random_pick_list_b)

Inside the for loop that is checking each character for the chosen letter. So, it prints out random_pick_list_b every time it finds a match.

Move it to right after the for loop if you want to do it one time when the checking is complete.

I would do this check once before the for loop.

Upvotes: 0

Related Questions