Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Word guessing game – how to print blanks

I am having trouble with making a guessing game. When you guess a correct letter it prints out the letter but not the blanks ().
I want it to look like this:
(guessed o)–> _ o
_ _ _
I am also wondering how to keep the guessed letter in after its turn.
like this:
_ o _ _ e _

    #Question 3 - Strings 
    #get guess from user
    print("Hello user! Would you like to guess what word I am thinking of? You may guess one letter at a time!")
    print("You also only have 10 guesses!")
    secret = "monkey"
    blanks = ["_"]*len(secret)
    print(*blanks)
    count = 10 

    while count > 0: 
        guess = input("What letter would you like to guess?")
        #check if the letter is in the word
        if guess in secret:
            location = secret.find(guess)
            newBlanks = secret[location] 
            print (newBlanks)
            count = count - 1
        #if NOT tell the player it isn't in the word
        else:
            print ("that letter is not in the word")
            count = count - 1

Any help would be appreciated 🙂

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You did not update blanks when a correct guess is made. This is why your guess isn’t being saved/displayed.

Code:

#Question 3 - Strings 
#get guess from user
print("Hello user! Would you like to guess what word I am thinking of? You may guess one letter at a time!")
print("You also only have 10 guesses!")
secret = "monkey"
blanks = ["_"]*len(secret)
print(*blanks)
count = 10 

while count > 0: 
    # if user guessed all letters, they win!
    if "".join(blanks) == secret:
        print("You win!")
        break
    guess = input("What letter would you like to guess?")
    #check if the letter is in the word
    if guess in secret:
        location = secret.find(guess)
        # store correct guess
        blanks[location] = guess
        # output new blanks -> sample: _ o _ _ _ _
        print(" ".join(blanks))
        count = count - 1
    #if NOT tell the player it isn't in the word
    else:
        print ("that letter is not in the word")
        count = count - 1

I also added a message to be printed when the user guesses the whole word.

Happy coding!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading