Skip to content
Permalink
1cc8bfe294
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
73 lines (58 sloc) 2.42 KB
def hangman():
import random
def randomword(secretwords):
return random.choice(secretwords)
def displayletters(words,guesses):
lettersstring = " "
for letter in words:
if letter in guesses:
lettersstring=lettersstring+letter
else:
lettersstring=lettersstring+"_"
return lettersstring
def aletters(guesses):
left = " "
for letter in "abcdefghijklmnopqrstuvwxyz":
if letter not in guesses:
left=left+letter
return left
def correctword(words,guesses):
for letter in words:
if letter not in guesses:
return False
return True
def main():
print(" H A N G M A N")
print("Are you ready?")
name =input("Enter your name\n")
print("Lets begin"+ " "+name+"!")
secretwords=("table","chair","bat","ball","gate","keys","hanger","belt","shoes","socks","metal","utensils","pencil","eraser","pin","phone","socket","keyboard","mouse","computer"," chip","cable","mathematics","english","science","work","money","solid","liquid","gas","date","weather","time","calendar")
words = randomword( secretwords)
word1=str(len(words))
print("Welcome to the Hangman game!")
print("Your word has {0} letters.\n".format(word1))
chances = int(word1)
guesses = []
while True:
print("Your chances: {0}".format(chances))
print("Available letters: {0}".format(aletters(guesses)))
guess = (input("Enter a letter: ")).lower()
if guess in guesses:
print("You used that letter already: {0}\n"
.format(displayletters(words, guesses)))
continue
if guess in words:
guesses.append(guess)
print("Good guess: {0}\n".format(displayletters(words,guesses)))
elif guess not in words:
guesses.append(guess)
print("Incorrect: {0}\n".format(displayletters(words,guesses)))
chances=chances-1
if correctword(words,guesses):
print("Congratulations, you won!")
break
if chances == 0:
print("You lost! The word was {0}".format(words))
break
main()
hangman()