Hangman
In this video, I guide you step-by-step on how to create a classic hangman game using the language of Python! The code is below. Watch it and have fun!
import random # Importing libraries
words = input("Type in the words that you would like me to pick from") # Ask what words the player wants to use
if words == '':
words = input("Please tell me what words to use") # If the player doesn't type any words when prompted, then ask again
print("You have unlimited tries to guess the word") # Let the player know that they have unlimited tries to guess the word
wrds = words.split() # Split the words that user types in based on wherever there is a space(use .split())
player = False # Set the player's turn to false(Now is computer's turn)
computer = random.choice(wrds) # Pick a random word from the words that the player has given
while player == False: # Loop that is used to code what happens in computer's turn
wrd1 = input("Guess my word('quit' for quit, 'hint' for hint, 'reset' for resetting the words)") # Let the player try to guess the computer's word
if wrd1 == "quit": # If the player types "quit", break the loop
print("Thanks for playing!")
break # Keyword in Python that means stop the running loop
elif wrd1 == "hint": # If the player types "hint", give how many letters is the computer's word
print("My word is {} letters long".format(len(computer)))
elif wrd1 == "reset": # If the player types "reset", ask the player again for words to pick from
words = input("Please tell me what your words are: ")
wrds = words.split() # Split words that the player provides
computer = random.choice(wrds) # Pick random word from the words that the player gave
print("Alright, I got it!") # Let the player know that the computer has picked a random word
elif wrd1 == computer: # If the player picks computer's word, then ask if the player wants to play again
guessed = input("You guessed it! Want to play again(y/n)?") # Ask the player if they want to play again
if guessed == "y": # If the player types "y", continue playing
computer = random.choice(wrds) # Pick a random word from the words that the player previously gave
continue # Keyword in Python that means continue running the loop
if guessed == "n": # If the player types "n", stop playing
print("We will play later, bye!")
break # Stop the running loop