Skip to content

Instantly share code, notes, and snippets.

@FaisalFehad
Last active December 31, 2016 15:58
Show Gist options
  • Save FaisalFehad/c27ea54a0975a75464ad96a59cc2b355 to your computer and use it in GitHub Desktop.
Save FaisalFehad/c27ea54a0975a75464ad96a59cc2b355 to your computer and use it in GitHub Desktop.
Hangman letter guessing game written in Python.
import sys
import random
import os
good_guesses = []
bad_guesses = []
def start():
clear()
print(game)
play_check = input("Welcome to the game. Would you like to start the game? Y/n ")
if play_check.lower() == "y":
play()
else:
clear()
print("Bye!")
sys.exit()
def play():
words = [
'apple',
'banana',
'orange',
'coconut',
'strawberry',
'lime',
'grapefruit',
'lemon',
'kumquat',
'blueberry',
'melon'
]
clear()
secrate_word = random.choice(words)
while len(bad_guesses) < len(secrate_word):
print("Good guesses: {} ".format(good_guesses))
print("Bad guesses: {} ".format(bad_guesses))
user_guess = input("Guess a letter: ").lower()
answer_check(user_guess, secrate_word, bad_guesses)
def answer_check(user_guess, secrate_word, bad_guesses):
if not user_guess.isalpha() or len(user_guess) != 1:
clear()
print("Wrong input:. You can only input one letter.")
elif user_guess in good_guesses:
clear()
print("You have alrady guessed '{}' it's right. Try another one.".format(user_guess))
elif user_guess in bad_guesses:
clear()
print("You have alrady guessed '{}' it's not right. Try another one.".format(user_guess))
elif user_guess in secrate_word:
clear()
print("Good guess!")
good_guesses.append(user_guess)
elif not user_guess in secrate_word:
clear()
print("Bad guess!")
bad_guesses.append(user_guess)
def clear():
if os.name == 'nt':
os.system('clr')
else:
os.system('clear')
while True:
start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment