Question
Code In Python
Well Document Code Every Line Please
* You cannot use any external Python libraries. You can, of course, import the libraries that are included in the standard Py
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Find the code, with comments on every line.

import random

# no of invalid guess possible, set as 8
n = 8

# set the game status as True when game is on.
# game_status will be False when the computer wins.
game_status = True

# list to keep track of letters said
history = []

# user defined function for guess
def guess_a_letter():
    # generate a random number between ord('A') = 65 and ord('Z') = 90
    # ord(a character) returns its ascii value
    guess = random.randint(ord('A'), ord('Z'))

    # convert the integer generated above to its corresponding ascii character.
    guess = chr(guess)

    # if the generated char is a repetition, try another one.
    while guess in history:
        guess = random.randint(ord('A'), ord('Z'))
        guess = chr(guess)

    # add generated character to history list
    history.append(guess)

    # return guess
    return guess

# read number of letters in the word as integer
word_length = int(input())

# word of unfilled letters
word = "_" * word_length

while (game_status):
    
    # get guess from function guess()
    guess = guess_a_letter()

    # print the random character generated
    print("I Guess {} (YES/NO): ".format(guess), end = "")

    # get YES/NO output from the user
    response = input()
    
    # user entered YES
    if response == 'YES':

        # get word with said letters filled
        print("Word: ", end = "")
        word = input()

        # set game status true, if word has an underscore (_)
        # user needs to enter word with underscores instead of hidden letters
        game_status = True if word.count('_')  != 0 else False

    # user enters anything other than YES -- NO or something else
    else:

    # decrement remaining invalid attempts
        n -= 1

        # set game status true if any more attempt left, else false
        game_status = True if n > 0 else False

# display end result, when game ends

#underscore coount = 0 means all letters are correctly guessed
if word.count("_") == 0:
    print("Computer Wins!")

# user wins, if computer ran out of all invalid attempts
else:
    print("You win!")
  • Number of invalid attempts set as 8, which you can change
  • User enter YES if the guess is right, NO or any other key if not
  • If the guess was right, user need to supply word with filled letters and _ for hidden letters. Feel free to change this also.

Read throught the comments. Feel free to reach out if any help is required.

Find example outputs below.

Word1 I guessed was PYTHON. And the second one was MILL.

6 I Guess Q (YES/NO): NO I Guess Z (YES/NO): NO I Guess L (YES/NO): NO I Guess N (YES/NO): YES Word: N I Guess E (YES/NO): NO 4 I Guess M (YES/NO): YES Word: M I Guess N (YES/NO): NO I Guess U (YES/NO): NO I Guess W (YES/NO): NO I Guess I (YES/NO): NO
Add a comment
Know the answer?
Add Answer to:
Code In Python Well Document Code Every Line Please * You cannot use any external Python...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Code In Python Well Document Code Every Line Please * You cannot use any external Python...

    Code In Python Well Document Code Every Line Please * You cannot use any external Python libraries. You can, of course, import the libraries that are included in the standard Python installation though. * Your software must use selection (if, if/ else, etc.) and repetitive (for, while, etc) control structures and have programmer defined functions. * Make sure you understand the code you submit since you will be expected to answer questions about it. The way the program should work...

  • Code In Python Well Document Code Every Line Please 5 Stars * You cannot use any...

    Code In Python Well Document Code Every Line Please 5 Stars * You cannot use any external Python libraries. You can, of course, import the libraries that are included in the standard Python installation though. * Your software must use selection (if, if/ else, etc.) and repetitive (for, while, etc) control structures and have programmer defined functions. * Make sure you understand the code you submit since you will be expected to answer questions about it. The way the program...

  • PYTHON CODE DOCUMENT EVERY LINE * You cannot use any external Python libraries. You can, of...

    PYTHON CODE DOCUMENT EVERY LINE * You cannot use any external Python libraries. You can, of course, import the libraries that are included in the standard Python installation though. * Your software must use selection (if, if/ else, etc.) and repetitive (for, while, etc) control structures and have programmer defined functions. * Make sure you understand the code you submit since you will be expected to answer questions about it. The way the program should work is the same as...

  • javafx assisantance confused on where to start Problem Description: (Game: hangman) Write a JavaFX program that...

    javafx assisantance confused on where to start Problem Description: (Game: hangman) Write a JavaFX program that lets a user play the hangman game. The user guesses a word by entering one letter at a time, as shown in Figure followings. If the user misses seven times, a hanging man swings, as shown in Figures. Once a word is finished, the user can press the Enter key to continue to guess another word. Guess a word: ***** God Missed letters Guess...

  • Do in Python please provide screenshots aswell. Here are the requirements for your game: 1. The...

    Do in Python please provide screenshots aswell. Here are the requirements for your game: 1. The computer must select a word at random from the list of avail- able words that was provided in words.txt. The functions for loading the word list and selecting a random word have already been provided for you in ps3 hangman.py. 2. The game must be interactive; the flow of the game should go as follows: • At the start of the game, let the...

  • Python Problem Hello i am trying to finish this code it does not save guesses or...

    Python Problem Hello i am trying to finish this code it does not save guesses or count wrong guesses. When printing hman it needs to only show _ and letters together. For example if the word is amazing and guess=a it must print a_a_ _ _ _ not a_ _ _ _ and _a_ _ _ separately or with spaces. The guess has a maximum of 8 wrong guesses so for every wrong guess the game must count it if...

  • PLEASE DO IN JAVA You can use any source for the dictionary or just a few...

    PLEASE DO IN JAVA You can use any source for the dictionary or just a few words are fine! description The purpose of this assignment is to practice with ArrayLists (and hopefully, you'll have some fun). As far as the user knows, play is exactly as it would be for a normal game of hangman, but behind the scenes, the computer cheats by delaying settling on a mystery word for as long as possible, which forces the user to use...

  • Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program...

    Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program 3 - Hangman Game Assignment purpose: User defined functions, character arrays, c style string member functions Write an interactive program that will allow a user to play the game of Hangman. You will need to: e You will use four character arrays: o one for the word to be guessed (solution) o one for the word in progress (starword) o one for all of...

  • Overview In this exercise you are going to recreate the classic game of hangman. Your program...

    Overview In this exercise you are going to recreate the classic game of hangman. Your program will randomly pick from a pool of words for the user who will guess letters in order to figure out the word. The user will have a limited number of wrong guesses to complete the puzzle or lose the round. Though if the user answers before running out of wrong answers, they win. Requirements Rules The program will use 10 to 15 words as...

  • C++ Hangman (game) In this project, you are required to implement a program that can play...

    C++ Hangman (game) In this project, you are required to implement a program that can play the hangman game with the user. The hangman game is described in https://en.wikipedia.org/wiki/Hangman_(game) . Your program should support the following functionality: - Randomly select a word from a dictionary -- this dictionary should be stored in an ASCII text file (the format is up to you). The program then provides the information about the number of letters in this word for the user to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT