Skip to content

Instantly share code, notes, and snippets.

@zemmyang
Created December 16, 2020 19:24
Show Gist options
  • Save zemmyang/eedce0e9637888b0a6d4bc854df3b959 to your computer and use it in GitHub Desktop.
Save zemmyang/eedce0e9637888b0a6d4bc854df3b959 to your computer and use it in GitHub Desktop.
Simple Hangman Python Game
import random
def welcome():
s = "hi!"
print(s)
def generate_word():
f = open("hangman_words.txt", "r")
x = []
for i in f:
x.append(i.strip())
getrand = random.randint(0, len(x))
return x[getrand]
def game_over():
s = '''
##### #######
# # ## # # ###### # # # # ###### #####
# # # ## ## # # # # # # # #
# #### # # # ## # ##### # # # # ##### # #
# # ###### # # # # # # # # #####
# # # # # # # # # # # # # #
##### # # # # ###### ####### ## ###### # #
'''
print(s)
def main():
welcome()
word = generate_word()
tries = 10
entries = []
printables = list('*'*len(word))
print("Your word is: " + ''.join(printables))
alive = True
while alive:
print("You have " + str(tries) + " guesses.")
isguessvalid = False
while not isguessvalid:
guess = input("Make a guess? >> ")
if not guess.isalpha():
print("Invalid input. Enter a character.")
isguessvalid = False
elif guess == word:
isguessvalid = True
alive = False
elif not len(guess) == 1:
print("Invalid input. Enter one character.")
isguessvalid = False
elif guess in entries:
print("You already tried that. Enter a new character.")
isguessvalid = False
else:
isguessvalid = True
print("You entered: " + guess)
entries.append(guess.lower())
if word.find(guess.lower()) == -1:
print("Nope, try again.")
tries = tries - 1
if tries == 0:
print("Na, you dead. Press F to pay respects.")
game_over()
print("The word was '" + word + "'.")
alive = False
elif alive == False: # i.e., user figured it out during guessing
print("Cool. You win. Bye.")
else:
print("Cool.")
for i in range(len(word)):
if word[i] == guess.lower():
printables[i] = guess.lower()
print("Your word is: " + ''.join(printables))
main()
transportation
independence
possibility
association
entertainment
advertising
conversation
understanding
interaction
information
preparation
environment
development
satisfaction
performance
manufacturer
requirement
supermarket
recognition
grandmother
explanation
replacement
administration
appointment
introduction
competition
refrigerator
maintenance
communication
championship
instruction
examination
distribution
contribution
perspective
imagination
consequence
construction
negotiation
organization
description
relationship
temperature
recommendation
combination
establishment
application
improvement
responsibility
measurement
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment