Skip to content

Instantly share code, notes, and snippets.

@saipraveenkondapalli
Last active April 18, 2020 16:34
Show Gist options
  • Save saipraveenkondapalli/aad11662610359d0b51ec1437961cdb1 to your computer and use it in GitHub Desktop.
Save saipraveenkondapalli/aad11662610359d0b51ec1437961cdb1 to your computer and use it in GitHub Desktop.
hangaman in python,with words importing from a file
'''
let's design the code for hangman.
'''
import time
import random
def secret_word():
with open("answers.txt","r") as f:
wordlist = f.read()
wordlist= wordlist.split()
big_words = []
for words in wordlist:
if len(words) == length:
big_words.append(words)
return random.choice(big_words)
def turn():
return len(ans) + 3
name = input("what is your name? \n ")
time.sleep(0.6)
length = int(input("choose the length of the word [4-10]"))
ans = secret_word()
print(f"hi,{name} let's play hangman.")
print(f"you are about play guess a {length} letter word ")
turns = turn()
guesses = ''
while turns > 0:
guess = input("guess a character: ")
guesses = guesses+guess
fail = 0
for char in ans:
if char in guesses:
print(char, end='')
else:
print('*', end='')
fail += 1
if fail == 0:
print("\n you won the game")
break
if guess not in ans:
turns -= 1
if turns == 0:
print("sorry, better luck next time")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment