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.

Next

This topic: Users/Ian > MobilePython > ActivityGame > ActivityGame1 > ActivityGame2 > ActivityGame3 > ActivityGame4 > ActivityGame5 > ActivityGame6
Topic revision: 20 Jan 2012, ian
Contact Us | Section Map | Disclaimer | RSS feed RSS FeedBack to top ^

Valid XHTML and CSS | Built on Foswiki

Page Updated: 20 Jan 2012 by ian. © Victoria University of Wellington, New Zealand, unless otherwise stated