Question

In: Computer Science

Write python code so that in a game of hangman the word ‘apple’ is displayed as...

Write python code so that in a game of hangman the word ‘apple’ is displayed as
“ _ _ _ _ _ “
and is usable code for any random words. Include in this code how to after the player has guessed a correct letter replace the _ with the correct guess shown as
“ _ p _ _ _”

Solutions

Expert Solution

import time
import random

# Accepts user name
playerName = input("Enter your name: ")

print ("Hello, " + playerName, " start the hangman game!")

print ("")

#wait for 1 second
time.sleep(1)

print ("Start guessing...")
time.sleep(0.5)

# Creates words
wordDic = ("danger", "apple", "easy", "democracy", "answer", "question")
# Generates random word
secretWord = random.choice(wordDic)

print(secretWord)

# creates an variable with an empty value to store the guess character
guessWord = ''

# Sets number of turns
turns = len(secretWord) + 1

# Create a while loop

#check if the turns are more than zero
while turns >= 0:

# make a counter that starts with zero
failed = 0   

# Loops every character in secretWord   
for char in secretWord:   

# Checks if the character is in the players guess
if char in guessWord:   
  
# Displays the character
print (char, end = '')

# if not found, print a dash symbol
else:
  
# Displays dash symbol separated by space
print ("_", end = ' ')

# Increase the failed counter with one
failed += 1   

# Checks if failed is equal to zero

# print You Won
if failed == 0:   
print ("\n You won")

# exit the script
break   

# Displays how many turns are left
print ("\n You have", + turns, 'more guesses')

# Accepts a guess character
guessCharacter = input("\n Guess a character: ")

# Concatenates the players guess character to guess word
guessWord += guessCharacter   

# Decrease the turn counter by one
turns -= 1
  
# Checks if the guess character is not found in the secret word
if guessCharacter not in secretWord:  

# Displays wrong
print ("Wrong")

# Checks if the turns are equal to zero
if turns == 0:
  
# Displays You Loose
print ("\n You Loose")

Sample Output:

Enter your name: Pyari
Hello, Pyari start the hangman game!

Start guessing...

_ _ _ _ _ _
You have 7 more guesses

Guess a character: t
Wrong
_ _ _ _ _ _
You have 6 more guesses

Guess a character: d
d_ _ _ _ _
You have 5 more guesses

Guess a character: g
d_ _ g_ _
You have 4 more guesses

Guess a character: n
d_ ng_ _
You have 3 more guesses

Guess a character: e
d_ nge_
You have 2 more guesses

Guess a character: r
d_ nger
You have 1 more guesses

Guess a character: a
danger
You won


Related Solutions

In python Using the example code from the HangMan program in our textbook, create a Word...
In python Using the example code from the HangMan program in our textbook, create a Word Guessing Game of your choice. Design a guessing game of your own creation. Choose a theme and build your words around that theme. Keep it simple. Note: I would highly recommend closely reviewing the HangMan code and program from Chapter 10 before starting work on this project. You can run the program via my REPL (Links to an external site.). Using python Similar to...
Please create a Hangman game in Java language. For this game a random word will be...
Please create a Hangman game in Java language. For this game a random word will be selected from the following list (abstract, cemetery, nurse, pharmacy, climbing). You should keep a running tab of the user’s score and display it on screen; the user begins with 100 points possible if they correctly guess the word without any mistakes. For every incorrect letter which is guessed, 10 points should be taken away from what is left of their total possible points. For...
Hangman We're going to write a game of hangman. Don't worry, this assignment is not nearly...
Hangman We're going to write a game of hangman. Don't worry, this assignment is not nearly as difficult as it may appear. The way hangman works (for this assignment - we are doing a simplified game) is as follows: the computer will choose a word. (For this version, a word is selected from a static list encoded into the program -- so the words are pretty limited). Let's say the word is "cocoa". the computer shows the user how many...
Python Programming: Scrambled Word Game Topics: List, tuple Write a scrambled word game. The game starts...
Python Programming: Scrambled Word Game Topics: List, tuple Write a scrambled word game. The game starts by loading a file containing scrambled word-answer pair separated. Sample of the file content is shown below. Once the pairs are loaded, it randomly picks a scrambled word and has the player guess it. The number of guesses is unlimited. When the user guesses the correct answer, it asks the user if he/she wants another scrambled word. bta:bat gstoh:ghost entsrom:monster Download scrambled.py (code featured...
Scrambled Word Game (Python) Topics: List, tuple In this lab, you will write a scrambled word...
Scrambled Word Game (Python) Topics: List, tuple In this lab, you will write a scrambled word game. The game starts by loading a file containing scrambled word-answer pair separated. Sample of the file content is shown below. Once the pairs are loaded, it randomly pick a scrambled word and have the player guess it. The number of guesses is unlimited. When the user guess the correct answer, it asks the user if he/she wants another scrambled word. bta:bat gstoh:ghost entsrom:monster...
How would I create a Hangman game that chooses  a random word and the player needs to...
How would I create a Hangman game that chooses  a random word and the player needs to guess it, letter by letter using  JavaScript and jQuery to allow user interaction. The content and the style of the page must be updated based on user’s input.
Write a program to allow a user to play the game Hangman. DO NOT USE AN...
Write a program to allow a user to play the game Hangman. DO NOT USE AN ARRAY The program will generate a random number (between 1 and 4581) to pick a word from the file - this is the word you then have to guess. Note: if the random number you generate is 42, you need the 42nd word - so loop 41 times picking up the word from the file and not doing anything with it, it is the...
Write a hangman program in c++.The game is played as follows: 1. The program selects a...
Write a hangman program in c++.The game is played as follows: 1. The program selects a random word for the user to guess (Read words for the user to guess into an array of strings from a file. The words are: revolutionary, meaning, recovery, compartment, trainer, pursuit, harm, platform, error, confusion) 2. User guesses one letter at a time until either they guess the word, or they run out of guesses. 3. Select a random word from the array of...
Create a Hangman game using C++ by QT creator. please separate each code that will insert...
Create a Hangman game using C++ by QT creator. please separate each code that will insert in hider files and cpp files and use an accurate screeshoots if needed. Thanks in advance.
PYTHON: Write the code to play a card game called Battle. Two players each have a...
PYTHON: Write the code to play a card game called Battle. Two players each have a card deck consisting of the following cards: two, three, four, … jack, queen, king, ace, in increasing order. One card deck could be represented by a list such as: cardsPlayer1 = ["two", "three", "four"..."jack", "queen", "king", "ace"] Both players have a card randomly selected. When a card is selected, remove it from the player’s deck. The player that plays the higher of the two...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT