[SOLVED] How to compare user inputted string to a character(single letter) of a word in a list?

Issue

I’m trying to create a hangman game with python. I know there is still a lot to complete but I’m trying to figure out the main component of the game which is how to compare the user inputted string (one letter) with the letters in one of the three randomly selected words.

import random

print("Welcome to hangman,guess the five letter word")
words =["china", "ducks", "glass"]
correct_word = (random.choice(words))
guess = input(str("Enter your guess:"))
guess_left = 10
guess_subtract = 1
if guess == "":
    guess_left = guess_left - guess_subtract
    print("you have" + guess_left + "guesses left")

Solution

I think you need a skeleton and then spend some time to improve the game.

import random

print "Welcome to hangman, guess the five letter word"

words = ["china", "ducks", "glass"]
correct_word = (random.choice(words))

trials = 10

for trial in range(trials):
    guess = str(raw_input("Enter character: "))

    if (len(guess) > 1):
        print "You are not allowed to enter more than one character at time"
        continue

    if guess in correct_word:
        print "Well done! '" + guess + "' is in the list!"
    else:
        print "Sorry " + guess + " does not included..."

Your next step could be print out something like c_i__ as well as the number of trials left. Have fun 🙂

When you finish with the implementation, take some time and re-implement it using functions.

Answered By – Daedalus

Answer Checked By – Candace Johnson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *