Welcome to Code Sharer, a code gallery for creating, saving and sharing all kinds of code snippets.

Python — Hangman

Posted On: Saturday, January 14, 2017 at 9:21 AM Last updated: Saturday, January 14, 2017 at 9:32 AM

This is a basic hangman game for python 2.7 there is one word, however, it would be simple to make a word bank to randomly choose words from.
import time

name = raw_input("Hello what is your name? ")
print "Hello, " + name, "ready to play hangman!"
print " "
print "Start guessing..."

#here we set the secret
word = "secret"

#creates an empty variable
guesses = ''

#determine the number of turns
turns = 10

# Create a while loop

#check if the turns are more than zero
while turns > 0:

# make a counter that starts with zero
failed = 0

# for every character in secret_word
for char in word:

# see if the character is in the players guess
if char in guesses:

# print then out the character
print char,

else:

# if not found, print a dash
print "_",

# and increase the failed counter with one
failed += 1

# if failed is equal to zero

# print You Won
if failed == 0:
print "
You won"

# exit the script
break

print

# ask the user go guess a character
guess = raw_input("guess a character:")

# set the players guess to guesses
guesses += guess


Ryan Bradley-Evans (Admin)

Joined on
Contributed 22 Snips

Get Social and Share

Tags