I started this hangman game as a suggestion for beginners but now I am stuck. The code runs with out any errors but I need some way to put a certain amount of turns that the user can have but I am not sure how I could add this to my loop? I know I would first create something like turns = 10. Right now all my code does is ask if you want to play then once you answer it shows the letters the word has then exits.So I need some help to get it to continue untill the word was guessed correct or the turns are up.

``````#!/usr/bin/env python

import random

words = ['sprinkles', 'computer', 'mouse', 'shooter', 'edge', 'guard', 'python']
myword = random.choice(words)
guesses = "aeiou"

while play:
print "Do you want to play a game?"
play = raw_input('Answer (y) yes or (n) no:')
if play == 'y':
print 'Let\'s begin'
print ""
for letter in random.choice(words):
if letter in guesses:
print letter,
else:
print '_',
break
if play == 'n':
print 'Good-Bye.'``````

you could change the 'while play' to something else that was an integer that equalled 10 and then every time the person has their turn you subtract one away from it until it is zero and the program ends.

``````import random

words = ['sprinkles', 'computer', 'mouse', 'shooter', 'edge', 'guard', 'python']
myword = random.choice(words)
guesses = "aeiou"
turns = 10

while turns:
turns-=1
print "Do you want to play a game?"
play = raw_input('Answer (y) yes or (n) no:')
if play == 'y':
print 'Let\'s begin'
print ""
for letter in random.choice(words):
if letter in guesses:
print letter,
else:
print '_',
break
if play == 'n':

print 'Good-Bye.'
break``````

Oh and also a quick question. Why are you breaking at line 20. Remember a break will end the loop forever so i dont see why you would want it there?

Building off of paulthom12345's code...

``````import random, sys

words = ['sprinkles', 'computer', 'mouse', 'shooter', 'edge', 'guard', 'python']
myword = random.choice(words)
guesses = "aeiou"
turns = 10

print "Do you want to play a game of hangman?"
play = raw_input('Answer (y) yes or (n) no:')
if play == 'y':
print 'Let\'s begin'
while turns:
blank_flag = False
turns-=1
print ""
for letter in myword:
if letter in guesses:
sys.stdout.write( letter )
else:
blank_flag = True
sys.stdout.write( '_' )
sys.stdout.write('\n')
sys.stdout.flush()
if not blank_flag:
print "You've won!!!  AMAZING!"
break
usr_inp = raw_input( "Enter your guess (only one letter): " )
if len(usr_inp) > 1:
print "You've forfeited a turn by entering more than one letter."
else:
guesses += usr_inp
if not turns:
print "GAME OVER!"
else:
print 'Good-Bye.'``````

Thanks alot! hope you don't mind if I take a little from both of your guys code and change it a little bit. and the reason I had the break in there was cause I thought I needed to break the first condition before I put another one....but as I understand now the first condition will only run if the condition is true then the next one if it is false? if that is still wrong maybe you could explain it.

Thanks alot! hope you don't mind if I take a little from both of your guys code and change it a little bit. and the reason I had the break in there was cause I thought I needed to break the first condition before I put another one....but as I understand now the first condition will only run if the condition is true then the next one if it is false? if that is still wrong maybe you could explain it.

As far as if, elif, else statements are concerned, Python evaluates them one by one.
Example:

``````if a1:
# Perform Action 1
elif a2:
# Perform Action 2
else:
# Perform Default Action
print 'End of if/else structure.. returning to main code'``````

If the condition a1 is true, action 1 will be performed and then processing will return to the print statement.
If a1 is not true, Python will evaluate a2. If a2 is true, action 2 will be performed. (Keep in mind that you can have as many elif conditions as you want.) Once action 2 is complete, processing will return to the print statement.
If none of the if or elif statements are true and an else clause exists (such as in this case), the default action will be performed. (You can always exclude this else clause and just not have anything happen unless one of your conditions is true).

I hope I've explained this concept well, but if not I urge you to look at Python's conditional processing docs

Oh and PS, break only breaks out of loops (ie, for, while).

Yes you did explain it very well I think I have a good understanding on how this works now and I appreciate all of your help...