-1

I am trying to make a hangman game. So far, I have a list of words for the program to randomly choose from:

a = random.choice(hangman_words))

Then I have the program display the number of characters in the word using the len function. That all works like it should. The problem I'm having is after prompting the user for a letter, I need the program to tell the user if the letter is correct or not and I don't know how to do that. Thanks

Kopernik
  • 2,783
  • 1
  • 20
  • 21
Lex
  • 1
  • 1
    Can you show us some code please? Also, please read this to know how to get the most out of SO: http://stackoverflow.com/help/how-to-ask – idjaw Oct 07 '15 at 17:08
  • 1
    Think of your word as an array of letters. Look at this question to help http://stackoverflow.com/questions/12934190/is-there-a-short-contains-function-for-lists-in-python – Kopernik Oct 07 '15 at 17:16
  • Perhaps a list of words is the wrong way to structure your data. If you use a string then you can simply do character in string like this `'h' in 'hello'`. – Brent C Oct 07 '15 at 17:16
  • A quick google search gives you plenty of examples to work with, from simple (http://www.pythonforbeginners.com/code-snippets-source-code/game-hangman) to more complex (https://inventwithpython.com/chapter9.html https://gist.github.com/DevDarren/4199441 http://code.activestate.com/recipes/578551-the-game-of-hangman-in-python/ ). Try and read those with a language guide nearby, and try and make one for yourself. – berna1111 Oct 07 '16 at 11:24

2 Answers2

1

maybe try an if statement? so for example

if userAnswer == theRandomLetter then
  print "Correct!"
else:
  print "Try again!"

and after that you could use a loop to repeat the process if the answer is incorrect

0

Here is some code to test your condition.

answer = raw_input("enter a letter:")

incorrect_letters = ["a","y","Y","q","Q","p"] 


for x in incorrect_letters:
    if answer == x:
        print "incorrect choice"

Here is some code to generate characters:

char_list = ["a","b","c","d","e","f","g","h","i","j","k","l","m",
            "n","o","p","q","r","s","t","u","v","w","x","y","z",
            "A","B","C","D","E","F","G","H","I","J","k","L","M",
            "N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]

num_char= 100

my_char = [random.choice(char_list) for i in xrange(num_char)]