Question

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 it reaches 8 the game must end.

def hangman(inf):
infile = open(inf,"r")
hanglist=[]
hide=[]
man=[]
import random
for line in infile:
hanglist.append(line)
word=random.choice(hanglist)#is a str
hide=len(word)
man=(hide-1)*' _ '
  
#close the file   
infile.close()
return (man,word)

#main driver program
print('insert dictionary.txt to start')
inf = input('what is your file? ')
#call the function
man1, word1 = hangman(inf)
print('welcome to Hangman')
game=input('Would you like to play?y/n')
while game=='y'and answer!=8:
print(word1)
answer=1
guess=input('Enter a character')
for i in range(0,len(word1)):#include guess?
if word1[i]==guess:
hman=list(man1)
hman[2*i+1]=guess# i=2*i+1
hman.remove( ' ', )
print(hman)
else:
answer=+1
print('not correct, you have'+str(answer-8)+' '+'left')

0 0
Add a comment Improve this question Transcribed image text
Answer #1

'''
Python version : 3.6
Python program to implement the game hangman
'''

import random

def hangman(inf):
  
   infile = open(inf,"r")
   hanglist=[]
   #hide=[]
   #man=[]
  
   for line in infile:
       hanglist.append(line)
       word=random.choice(hanglist)#is a str
      
   man = ''
   # word to display
   for e in word:
       man= man+'_'+' '          
   #close the file   
   infile.close()
   return (man,word)
  
#main driver program
print('insert dictionary.txt to start')
inf = input('what is your file? ')
#call the function
man1, word1 = hangman(inf)
word1 = word1.lower() # convert the word to lowercase
print('welcome to Hangman')
game= input('Would you like to play?y/n ')
answer = 0
guesses = []
print(man1)
# loop continues till user has not guessed the word or attempts have not been exhausted
while game.lower()=='y'and answer < 8 and '_' in man1:
   #print(word1)
  
   print('Letters guessed so far : '+str(guesses))
   guess= input('Enter a character : ')
   # check if character has been guessed earlier, then re-prompt the user for another character
   while guess.lower() in guesses:
       print('You have already guessed the letter')
       guess= input('Enter a character : ')
   guesses.append(guess.lower())  
   # if character is present in the word
   if guess.lower() in word1:
       # loop to replace '_' with the character
       for i in range(0,len(word1)):#include guess?
           if word1[i]==guess:
               man1 = man1[0:2*i] + word1[i]+man1[2*i+1:len(man1)]              
   else: # character not present in word
       answer += 1 # add 1 to incorrect answer
       print('not correct, you have'+str(answer-8)+' '+'left')
   # print the updated word to display
   print(man1)
# display the result  
if '_' in man1:
   print("Sorry!! you couldn't guess the word.The word was "+word1)
else:
   print("Congratulations!! you guessed the word.The word was "+word1)
#end of program

Code Screenshot:

Output:

Add a comment
Know the answer?
Add Answer to:
Python Problem Hello i am trying to finish this code it does not save guesses or...
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
  • Help please, write code in c++. The assignment is about making a hangman game. Instructions: This...

    Help please, write code in c++. The assignment is about making a hangman game. Instructions: This program is part 1 of a larger program. Eventually, it will be a complete Hangman game. For this part, the program will Prompt the user for a game number, Read a specific word from a file, Loop through and display each stage of the hangman character I recommend using a counter while loop and letting the counter be the number of wrong guesses. This...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

  • on python i need to code a guessing game. After the player has guessed the random...

    on python i need to code a guessing game. After the player has guessed the random number correctly, prompt the user to enter their name. Record the names in a list along with how many tries it took them to reach the unknown number. Record the score for each name in another list. When the game is quit, show who won with the least amount of tries. this is what i have so far: #Guess The Number HW assignment import...

  • HEy GUYS PLZ I WROTE THIS CODE BUT I WAS ASKED TO ADD SOME ASSERTION TESTS AND I HAVE NEVER DONE SUCH THING. CAN YOU GUY...

    HEy GUYS PLZ I WROTE THIS CODE BUT I WAS ASKED TO ADD SOME ASSERTION TESTS AND I HAVE NEVER DONE SUCH THING. CAN YOU GUYS HELPS ME OUT ON HOW TO TEST A SIMPLE CODE SINCE ITS A GUESSING GAME! THANK YOU. PYTHON import random # here it allows us to use the benefits of the functions random #function 1 which is just a screen organized wordings def screen): screeninstructions () name input("Your Name : ") print('Welcome, name, 'This...

  • Hello, I am trying to solve the following problem using python: Assignment 3: Chatbot A chatbot...

    Hello, I am trying to solve the following problem using python: Assignment 3: Chatbot A chatbot is a computer program designed to emulate human conversation. For this program you will use if statements, user input, and random numbers to create a basic chatbot. Here is the scenario: You have decided to start an online website. You are creating a prototype to show investors so you can raise money and launch your website. You should ask the user at least 5...

  • how would i write my code in Pseudocode? I am pretty much a beginner, therefore this...

    how would i write my code in Pseudocode? I am pretty much a beginner, therefore this is my first major computer science assignment and im not really understanding the written way of code.. please help and explain. Thank you! this is my working code: # Guess The Number HW assignment import random guessesTaken = 0 names=[] tries=[] print("Welcome! ") question = input("Would you like to play the guessing game? [Y/N]") if question == "N": print("Sorry to see you go so...

  • I need help fixing my python3 code. I am trying to get my Monty hall game...

    I need help fixing my python3 code. I am trying to get my Monty hall game working correctly. Below is my code. When I run the code it repeats the first question and doesn't work until the 3rd attempt or sometimes more than that. How do I fix this? I also need to be able to make it run 5 times in a row.(like ask the user at least 5 times, so it is re-playable. I added a image of...

  • 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...

  • This is python code I am having trouble with the both winning messages printing if the...

    This is python code I am having trouble with the both winning messages printing if the user guesses in 1 try. 1 message or the other should print depending on the number of guesses not both. Thank you for your help! import random answer = 5 tries = 0 user_guess = "" while True: user_guess = int(input("enter a number between 1 and 10: ")) tries = tries + 1    if user_guess == answer: print ("You win! \nIt took you",...

  • I have to use java programs using netbeans. this course is introduction to java programming so...

    I have to use java programs using netbeans. this course is introduction to java programming so i have to write it in a simple way using till chapter 6 (arrays) you can use (loops , methods , arrays) You will implement a menu-based system for Hangman Game. Hangman is a popular game that allows one player to choose a word and another player to guess it one letter at a time. Implement your system to perform the following tasks: Design...

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