Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

USE THIS LAYOUT AND PYTHON layout below: from random import choice, random dictionary_file = dictionary.txt # make a dictionary.txt in the same folder where hangman.py

USE THIS LAYOUT AND PYTHON

layout below:

from random import choice, random dictionary_file = "dictionary.txt" # make a dictionary.txt in the same folder where hangman.py is located #dictionary_short.txt consists of ad bat card dress engine T-shirt gasoline gathering evaluation self-esteem unemployment # write all your functions here # make a dictionary from a dictionary file ('dictionary.txt', see above) # dictionary keys are word sizes (1, 2, 3, 4, , 12), and values are lists of words # for example, dictionary = { 2 : ['Ms', 'ad'], 3 : ['cat', 'dog', 'sun'] } # if a word has the size more than 12 letters, put it into the list with the key equal to 12 def import_dictionary (filename) : dictionary = {} max_size = 12 try : pass except Exception : pass return dictionary # print the dictionary (use only for debugging) def print_dictionary (dictionary) : max_size = 12 pass # get options size and lives from the user, use try-except statements for wrong input def get_game_options () : pass return (size, lives) # MAIN if __name__ == '__main__' : # make a dictionary from a dictionary file dictionary = import_dictionary(dictionary_file) # print the dictionary (use only for debugging) print_dictionary(dictionary) # remove after debugging the dictionary function import_dictionary # print a game introduction # START MAIN LOOP (OUTER PROGRAM LOOP) # set up game options (the word size and number of lives) # select a word from a dictionary (according to the game options) # use choice() function that selects an item from a list randomly, for example: # mylist = ['apple', 'banana', 'orange', 'strawberry'] # word = choice(mylist) # START GAME LOOP (INNER PROGRAM LOOP) # format and print the game interface: # Letters chosen: E, S, P list of chosen letters # __ P P __ E lives: 4 XOOOO hidden word and lives # ask the user to guess a letter # update the list of chosen letters # if the letter is correct update the hidden word, # else update the number of lives # and print interactive messages # END GAME LOOP (INNER PROGRAM LOOP) # check if the user guesses the word correctly or lost all lives, # if yes finish the game # END MAIN LOOP (OUTER PROGRAM LOOP) # ask if the user wants to continue playing, # if yes start a new game, otherwise terminate the program

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Hangman Game Objective: practicing with lists, dictionaries, files, exceptions, loops, elif statements, functions, and string methods: strip(), join(), isalpha(), upper(), or lower(). Description In this assignment you will write a program for a popular game Hangman. Your program will generate a legitimate dictionary word and ask the user to guess the aware that your program output should match this format exactly. Please read the instructions carefully and completely before starting to wor program! In the beginning a program should output the following messages: Welcome to the Hangman Game! Please choose a size of a word to be guessed [3 - 12, default any size]: If the user chooses 7 then the program prints: The word size is set to 7. Please choose a number of lives [110, default 5]: If the user chooses 3 then the program prints: You have 3 lives. If the user does not enter the valid input (for example, the user hits just "Enter" key or chooses numbers outside of the specified range of 3 - 12 ) the program should set up default parameters and inform the user about them: Please choose a size of a word to be guessed [3 - 12, default any size]: A dictionary word of any size will be chosen. Please choose a number of lives [110, default 5]: If the user does not enter the valid input (for example, the user hits just "Enter" key or chooses numbers outside of the specified range of 3 - 12 ) the program should set up default parameters and inform the user about them: Please choose a size of a word to be guessed [ 3 - 12, default any size]: A dictionary word of any size will be chosen. Please choose a number of lives [1 - 10, default 5]: You have 5 lives. underscores, the number of lives left shown as an uppercase letter O, and a prompt to choose a new letter: Letters chosen: _ _ _ - lives: 500000 Please choose a new letter > Let's assume that the dictionary word APPLE has been selected by the program. If the user enters the first letter E, then the program outputs these lines: You guessed right! Letters chosen: E _ _ _ E lives: 500000 Please choose a new letter > If the user enters the second letter S, then the program outputs the following lines: You guessed wrong, you lost one life. Letters chosen: E,S _ _ _ E lives: 4 XO000 Please choose a new letter > If the user enters the third letter P, then the program outputs this: You guessed right! Letters chosen: E,S,P - PP - E lives: 40000 Please choose a new letter > If the user enters again letter E, the program print the message: You have already chosen this letter. Please choose a new letter > If the user lost all lives, the program prints the following messages: You guessed wrong, you lost one life. Letters chosen: E,S,P,B,T,G,O - PP - E lives: XXXXXX You lost! The word is APPLE! Would you like to play again [Y/N]? If the user won, the program prints these messages: You guessed right! Letters chosen: E,S,P,A,L A P P L E lives: 40000 Congratulations!!! You won! The word is APPLE! Would you like to play again [Y/N]? If a word has a hyphen, then the hyphen should be shown in the beginning of the program. For example, if the hidden word is T-shirt, then the hidden word should be displayed as shown below: ------lives:500000 Programming Approaches Your source file should be named hangman.py and should start with the header (a comment block) where you write important information about the source file including who created it, when it is created, what the purpose of it, and how to use it. You are also required to implement two functions: import_dictionary(filename) and get_game_options(). The function import_dictionary(filename) should create a dictionary from a text file (a dictionary file) that is provided to you (See below). The dictionary file contains legitimate 2,542 words from an English vocabulary, each word is written on a separate line. Your program should read the file line by line and ad words to the dictionary. The function should return the dictionary. The function get_game_options() should ask the user to select game options such as a dictionary word size and a number of lives. The function should return a tuple of selected game options (a word size and a number of lives). credit. A dictionary is used for storing a vocabulary sorted by word size, and at least three lists are used for storing letters of the hidden word, guessed letters, and words of the same size (as dictionary values). If you prefer, you can use the following template file hangman.py. You can download it from Files/Programming Assignments/PA1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions