Question
This assignment involves coding and testing of a program that facilitates playing the Hangman word game. Hangman is a popular word game. In this game,
This assignment involves coding and testing of a program that facilitates playing the Hangman word game.
Hangman is a popular word game. In this game, the player is given some number of blanks representing the name of a movie or an actor and he/she has to guess the name using at most k number of chances.
Goals
1) Understand how continuous user interaction can be established using an infinite loop
2) Practice the use of various string methods to facilitate displaying changing info to users
Task
Your task is to implement the Hangman game in Python 3. Make sure you understand the rules of the game.
Program Specifications:
- Output a brief description of the game of Hangman and how to play
- Choose a word or phrase to be guessed from a hardcoded list of words (stored in a list or input from a text file you create)
- Output the appropriate number of dashes and spaces to represent the phrase (make sure its clear how many letters are in each word and how many words there are)
- Continuously read guesses of a letter from the user and fill in the corresponding blanks if the letter is in the word, otherwise, report that the user has made an incorrect guess.
- Each turn you will display the phrase as dashes but with any already guessed letters filled in, as well as which letters have been incorrectly guessed so far and how many guesses the user has remaining.
- Your program should allow the user to make a total of 10 incorrect guesses.
- You MUST use at least 3 string methods or operators in a useful manner (several examples that I used are given in the notes below). If you wish to use lists in your project that is fine, as long as you meet this requirement.
Notes
If the letter has already been guessed, output a message to the player and ask for input again.
If the guess entered is not an alphabetic letter, output a message and ask for input again.
If the letter is present in the word to be guessed, fill in the blanks appropriately with this particular letter. If the complete name has been guessed, the game is over - player wins the game. Output a message telling the player they have won and quit the game.
If the letter/digit is not present in the word to be guessed, give a message to the player indicating that the guess is incorrect and the remaining number of chances is one less. If the remaining number of chances is 0 (zero), the game is over - the player loses the game. Output a message that they have lost and what the correct word was. Quit the game.
Tips This project deals significantly with strings, use help(str) in the Python shell window to see all of the string methods that may be useful.
1) The string concatenation operator +. In order to keep track of the incorrect guesses, you could initialize a blank string and use + to update the string with the incorrect guesses.
2) The membership operator in would be useful to check if a particular letter/digit has already been guessed (correctly or incorrectly).
3) String slicing is useful to insert a letter in a string. For example, if I have x = hello and I wanted to put a Z in the middle of the word, I could write:
x = x[0:3] + Z + x[3:]
or if I wanted to Z to replace the e I could write:
x = x[0:1] + Z + x[2:]
remember string indexing using slices includes the start position but not the end position, so
x[0:2] is he but does not include the l at index 2.
4) lower() can be used to change a string to lowercase make sure you find the letter the user enters whether its lower or uppercase in the guess word.
5) find() returns the index at which the first instance of a substring is found in a string, for example if x=hello
x.find(e) returns 1 and x.find(l) returns 2.
6) remember, if there is an apostrophe () or a dash (-) in the original phrase you should output it instead of a dash, the user doesnt have to guess those.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started