Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#starter code import random CHAR_PLACEHOLDER = '-' def main(): file_name = input(Enter filename: ) file_object = open_file(file_name) if file_object is not None: random_seed() word_list =

image text in transcribedimage text in transcribed

#starter code

import random

CHAR_PLACEHOLDER = '-'

def main(): file_name = input("Enter filename: ") file_object = open_file(file_name) if file_object is not None: random_seed() word_list = # your function call secret_word = get_secret_word(word_list) guess_word = initialize_guess_word(secret_word) # Your code else: print("File {} not found!".format(file_name))

def open_file(filename): '''Opens the given file, returning its file object if found, otherwise None''' try: file_object = open(filename, 'r') return file_object except FileNotFoundError: return None

def random_seed(): '''Initializes the random number generator''' seed = int(input("Random seed: ")) random.seed(seed)

def get_secret_word(word_list): '''Returns a random secret word from the given word list''' secret_word = random.choice(word_list) return secret_word

def initialize_guess_word(secret_word): '''Returns an initialized guess word in the form of a list''' guess_word = [CHAR_PLACEHOLDER]*len(secret_word) return guess_word

# Main program starts here if __name__ == "__main__": main()

Write a Python program, hangman.py, that allows a user to play the game Hangman. In the game, the computer selects a word from a file and the user tries to guess the word by iteratively suggesting its individual letters. In this implementation of the game the following holds: The user has at most 12 tries when guessing individual letters of a word. The collection of words from which the computer selects is stored in a file which is read by the program. The word to be guessed is selected from the collection by using the function random.choicel). The functions which selects the word is given. You are NOT allowed to use any other import statement than the one which is given. Skrrnar, sem geyma safn ora, innihalda eitt or (linu, td: / The files that contain word collections store a single word in each line, eg.: lion umbrella window computer glass juice chair desktop laptop dog cat lemon cabel mirror hat Enter filename: words1.txt Random seed: 10 The secret word has 3 characters Guess 1 of 12 Word to guess: - - Choose a letter: a Incorrect letter! Guess 2 of 12 Word to guess: Choose a letter: i Incorrect letter! Guess 3 of 12 Word to guess: --- Choose a letter: 0 You guessed correctly! Guess 4 of 12 Word to guess: -O- Choose a letter: s

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Intelligent Information And Database Systems Asian Conference Aciids 2012 Kaohsiung Taiwan March 19 21 2012 Proceedings Part 3 Lnai 7198

Authors: Jeng-Shyang Pan ,Shyi-Ming Chen ,Ngoc-Thanh Nguyen

2012th Edition

3642284922, 978-3642284922

More Books

Students also viewed these Databases questions