prev next 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Input with "raw_input"

Our game is no good if you can't guess. To let the player guess we will use a function called "raw_input()."

It works like this:


answer = raw_input('my question')

So try changing your program to look like this:
#!/usr/bin/env python

print 'time to play hangman'
secret = 'crocodile'
guesses = 'aeiou'

guess = raw_input('guess a letter: ')
guesses += guess

for letter in secret:
  if letter in guesses:
    print letter,
  else:
    print '_',

The "guesses += guess" line add the guess to the string of guesses. If the string of guesses was "aeiou" and the new guess is "c", then the string of guesses will become "aeiouc".

Let's run it.


laptop:~ student$ Desktop/game.py
guess a letter: c
c _ o _ o _ i _ e
laptop:~ student$  

You can guess any letter and it will show it to you.