jeudi 30 avril 2015

Hangman: problems with code

I recently started learning Python and am trying to make a hangman program.

The error comes up at for char in word and says that "name 'word' is not defined".

The code:

import random
import time

r = random.randint(0, 4)
name = raw_input("what is your name? ")
print "hello, " + name, "time to play hangman\n"
time.sleep(1)
print "ok take a guess:"
time.sleep(0.5)
wordList = ['apples', 'banana', 'orange', 'blueberry', 'grapefruit']
word = wordList[r]
guesses = 0
turns = 10
while turns > 0:         
failed = 0             
for char in word:      
    if char in guesses:    
        print char,    
    else:
        print "_",
        failed += 1    
if failed == 0:        
    print "\nYou won"  
break              
print
theGuess = raw_input("guess a character:")
guesses += theGuess                    
if theGuess not in word:  
    turns -= 1        
    print "Wrong\n"    
    print "You have", + turns, 'more guesses' 
    if turns == 0:           
        print "You Loose\n"




Aucun commentaire:

Enregistrer un commentaire