Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, I am trying to make a Hangman type game for python. This program generates a

ID: 3825759 • Letter: H

Question

Hi, I am trying to make a Hangman type game for python. This program generates a random string
and then asks the user to try and solve. I would ideally like for the program to remove (-) spaces and replace it with the correct letter. Here is what I have so far:

from random import choice
from string import ascii_uppercase


welcome = ('Welcome to "Guess This String"')

print (welcome)

randWord = (''.join(choice(ascii_uppercase) for i in range(10)))

blanks = '_ ' * len(randWord)
print ()
print ("Word: ",blanks)

count = 10
guessed = ""
while True:
guess = input ("Please make a guess: ")   
if len(guess) != 1:
print ("Please enter only one letter at a time")
elif guess not in 'abcdefghijklmnopqrstuvwxyz':
print ('Please only guess letters "(A to Z)"')

if guess in randWord:
# ... print 'correct', else 'not correct', decrease count

newBlanks = " ".join(c if c in guessed else "_" for c in randWord)
print("Word: ",newBlanks)

else:
count -=1
print ("Guess is wrong! ", count, " more failed attempts allowed.")
print()
print("Word: ",newBlanks)

Explanation / Answer

from random import choice
from string import ascii_uppercase

welcome = ('Welcome to "Guess This String"')
print (welcome)
randWord = (''.join(choice(ascii_uppercase) for i in range(10)))
print (randWord)
blanks = '_ ' * len(randWord)
print ()
print ("Word: ",blanks)
count = 0
num = 10
guessed = ""
continueLoop = True
newBlanks = []

while continueLoop == True:
   guess = raw_input ("Please make a guess: ")
   if len(guess) != 1:
       print ("Please enter only one letter at a time")
   #elif guess not in 'abcdefghijklmnopqrstuvwxyz':
   elif guess not in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
       print ('Please only guess letters "(A to Z)"')
   elif guess in randWord:
       for index, c in enumerate(randWord):
           if c in guess and count ==0:
               newBlanks.append(c)
           elif count == 0:
               newBlanks.append("_ " )
           elif c in guess:
               newBlanks[index: index+1] = c
       count = count + 1
       guessed   = "".join(newBlanks)
       if "_ " in guessed:
           continueLoop = True
           print(guessed)
       else:
           print (guessed)
           continueLoop = False
   else:
       num -=1
       print ("Guess is wrong! ", count, " more failed attempts allowed.")
       print()
       print("Word: ",newBlanks)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote