raspberry.pi
raspberry.pi

Reputation: 593

Hangman game trouble with keep_playing

So I'm new to python and went trough a tutorial to write a hangman game in python 2.7 on my new pi. Well, anyway I liked the code and everything, and it ran fine but then I wanted to add something to make it ask ,"if you wanted to continue playing", and I did a lot of research and talked on some chat rooms about it and came up with/found this script to exit:

while keep_playing == False:
user = raw_input("\n\tShall we play another game? [y|n] ")
again = "yes".startswith(user.strip().lower())
if again:
    keep_playing = True
if not again:
    break

raw_input ("\n\n\nPress enter to exit")

but I get this error:

Traceback (most recent call last):
File "/home/pi/Desktop/Scripts/scrappy/ls/ls/hangman3.py", line 40, in <module>
   while keep_playing == False:
NameError: name 'keep_playing' is not defined

when its ran with this script

import random
import urllib

print 'time to play hangman'
   animals = urllib.urlopen('http://davidbau.com/data/animals').read().split()
secret = random.choice(animals)
guesses = 'aeiou'
turns = 5

while turns > 0:
missed = 0
for letter in secret:
  if letter in guesses:
    print letter,
  else:
    print '_',
    missed += 1

print

 if missed == 0:
   print 'You win!'
    break

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

 if guess not in secret:
    turns -= 1
    print 'Nope.'
    print turns, 'more turns'
    if turns < 5: print '   O   '
    if turns < 4: print ' \_|_/ '
    if turns < 3: print '   |   '
    if turns < 2: print '  / \  '
    if turns < 1: print ' d   b '
    if turns == 0:
      print 'The answer is', secret

while keep_playing == False:
user = raw_input("\n\tShall we play another game? [y|n] ")
again = "yes".startswith(user.strip().lower())
if again:
    keep_playing = True
if not again:
    break

raw_input ("\n\n\nPress enter to exit")

Can anyone help me? ****edit***** someone can close this tread using the tips provided i have solved my problem this is the final code

import random
import urllib

animals = urllib.urlopen('http://davidbau.com/data/animals').read().split()

while True:

print 'time to play hangman'
secret = random.choice(animals)
guesses = 'aeiou'
turns = 5

while turns > 0:
    missed = 0
    for letter in secret:
        if letter in guesses:
            print letter,
        else:
            print '_',
            missed += 1

    print

    if missed == 0:
        print 'You win!'
        break

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

    if guess not in secret:
        turns -= 1
        print 'Nope.'
        print turns, 'more turns'
        if turns < 5: print '   O   '
        if turns < 4: print ' \_|_/ '
        if turns < 3: print '   |   '
        if turns < 2: print '  / \  '
        if turns < 1: print ' d   b '
        if turns == 0:
            print 'The answer is', secret
            break


user = raw_input("\n\tShall we play another game? [y|n] ")
again = "yes".startswith(user.strip().lower())
if not again:
    raw_input ("\n\n\nPress enter to exit")
    break

Upvotes: 1

Views: 166

Answers (2)

Joe Michail
Joe Michail

Reputation: 449

animals = urllib.urlopen('http://davidbau.com/data/animals').read().split()
secret = random.choice(animals)
guesses = 'aeiou'
turns = 5
keep_playing = False 

if guess not in secret:
    turns -= 1
    print 'Nope.'
    print turns, 'more turns'
    if turns < 5: print '   O   '
    if turns < 4: print ' \_|_/ '
    if turns < 3: print '   |   '
    if turns < 2: print '  / \  '
    if turns < 1: print ' d   b '
    if turns == 0:
      print 'The answer is', secret
      keep_playing  = False

This should do it

Upvotes: 1

Ruudt
Ruudt

Reputation: 870

I'm not into python, but I can see you're indeed trying to compare an empty/undefined variable "keep_playing" to false. To my knowledge, you can't compare a variable to something, if you haven't declared the variable before the comparison, not sure if this is different in Python though.

What happens if you write this line along the other variables:

keep_playing = False

so you'll get :

animals = urllib.urlopen('http://davidbau.com/data/animals').read().split()
secret = random.choice(animals)
guesses = 'aeiou'
turns = 5
keep_playing = False

Upvotes: 1

Related Questions