×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

Python Program to Create a Hangman Game Assignment Solution

June 28, 2024
Dr. Nicholas Scott
Dr. Nicholas
🇯🇵 Japan
Python
Dr. Nicholas Scott, a distinguished Computer Science expert, holds a PhD from Tokyo University, Japan. With an impressive 8 years of experience, he has completed over 600 Python assignments, showcasing his unparalleled expertise and dedication to the field.
Key Topics
  • Instructions
  • Requirements and Specifications
Tip of the day
Ensure you understand the dataset thoroughly before starting your machine learning assignment. Visualize the data, check for missing values, and identify patterns or anomalies to guide your model-building process effectively.
News
In 2024, universities have introduced new programming courses focusing on cybersecurity, machine learning, and artificial intelligence to better prepare students for modern IT careers.

Instructions

Objective
Write a Python assignment that involves creating a hangman game. This classic word-guessing game can be a great way to practice your coding abilities while having fun. Implement the logic to choose a random word, allow user input for guessing letters, and keep track of incorrect guesses. Enhance the assignment by adding features like limiting the number of guesses, displaying the hangman figure as guesses go wrong, and providing a user-friendly interface. This assignment will not only sharpen your Python programming capabilities but also teach you about data handling, user interaction, and logical thinking.

Requirements and Specifications

program to create a hangman game in python
program to create a hangman game in python 1

Source Code

# Problem Set 2, hangman.py

# Name:

# Collaborators:

# Time spent:

import random

import string

# -----------------------------------

# HELPER CODE

# -----------------------------------

WORDLIST_FILENAME = "words.txt"

def load_words():

"""

returns: list, a list of valid words. Words are strings of lowercase letters.

Depending on the size of the word list, this function may

take a while to finish.

"""

print("Loading word list from file...")

# inFile: file

inFile = open(WORDLIST_FILENAME, 'r')

# line: string

line = inFile.readline()

# wordlist: list of strings

wordlist = line.split()

print(" ", len(wordlist), "words loaded.")

return wordlist

def choose_word(wordlist):

"""

wordlist (list): list of words (strings)

returns: a word from wordlist at random

"""

return random.choice(wordlist)

# -----------------------------------

# END OF HELPER CODE

# -----------------------------------

# Load the list of words to be accessed from anywhere in the program

wordlist = load_words()

def has_player_won(secret_word, letters_guessed):

"""

secret_word: string, the lowercase word the user is guessing

letters_guessed: list (of lowercase letters), the letters that have been

guessed so far

returns: boolean, True if all the letters of secret_word are in letters_guessed,

False otherwise

"""

# FILL IN YOUR CODE HERE AND DELETE "pass"

pass

def get_word_progress(secret_word, letters_guessed):

"""

secret_word: string, the lowercase word the user is guessing

letters_guessed: list (of lowercase letters), the letters that have been

guessed so far

returns: string, comprised of letters and asterisks (*) that represents

which letters in secret_word have not been guessed so far

"""

# FILL IN YOUR CODE HERE AND DELETE "pass"

pass

def get_available_letters(letters_guessed):

"""

letters_guessed: list (of lowercase letters), the letters that have been

guessed so far

returns: string, comprised of letters that represents which

letters have not yet been guessed. The letters should be returned in

alphabetical order

"""

# FILL IN YOUR CODE HERE AND DELETE "pass"

pass

def hangman(secret_word, with_help):

"""

secret_word: string, the secret word to guess.

with_help: boolean, this enables help functionality if true.

Starts up an interactive game of Hangman.

* At the start of the game, let the user know how many

letters the secret_word contains and how many guesses they start with.

* The user should start with 10 guesses.

* Before each round, you should display to the user how many guesses

they have left and the letters that the user has not yet guessed.

* Ask the user to supply one guess per round. Remember to make

sure that the user puts in a single letter (or help character '$'

for with_help functionality)

* If the user inputs an incorrect consonant, then the user loses ONE guess,

while if the user inputs an incorrect vowel (a, e, i, o, u),

then the user loses TWO guesses.

* The user should receive feedback immediately after each guess

about whether their guess appears in the computer's word.

* After each guess, you should display to the user the

partially guessed word so far.

-----------------------------------

with_help functionality

-----------------------------------

* If the guess is the symbol $, you should reveal to the user one of the

letters missing from the word at the cost of 3 guesses. If the user does

not have 3 guesses remaining, print a warning message. Otherwise, add

this letter to their guessed word and continue playing normally.

Follows the other limitations detailed in the problem write-up.

"""

# FILL IN YOUR CODE HERE AND DELETE "pass"

pass

# When you've completed your hangman function, scroll down to the bottom

# of the file and uncomment the lines to test

if __name__ == "__main__":

# To test your game, uncomment the following three lines.

# secret_word = choose_word(wordlist)

# with_help = False

# hangman(secret_word, with_help)

# After you complete with_help functionality, change with_help to True

# and try entering "$" as a guess!

###############

# SUBMISSION INSTRUCTIONS

# -----------------------

# It doesn't matter if the lines above are commented in or not

# when you submit your pset. However, please run ps2_student_tester.py

# one more time before submitting to make sure all the tests pass.

pass

Related Samples

Explore our Python Assignments Sample Section to see expertly crafted solutions. From basic Python scripts to complex data analysis projects, these samples showcase our proficiency in delivering high-quality programming solutions. Find inspiration and insights for your assignments today!