Question

It's a Python question, I got confused which I cannot get the same result as the...

It's a Python question, I got confused which I cannot get the same result as the example below.

An example interaction can look like this:

>>> Welcome to Hangman!
_ _ _ _ _ _ _ _ _
>>> Guess your letter: S
Incorrect!
>>> Guess your letter: E
E _ _ _ _ _ _ _ E

_____________mycode___________________

word = 'AABBCC'
word = list(word)
wordlist = ['_' for i in range(len(word))]

  
if __name__ == '__main__':
print('Welcome to Hangman!')
guess = input('Guess your letter: ')
guess.upper()
  
# count that how many times to reach the final ans;
# but first to init;
count = 0
  
repeat = True
while repeat:
  
# if guess letter in the wordlist
if guess.upper() in wordlist:
count += 1
print('Invalid input, the letter already exits.')
guess = input('Guess your letter: ')
  
if guess.upper() in word:
count += 1
for index in range(len(word)):
if guess.upper() == word[index]:
index = word.index(guess.upper())
wordlist[index] = guess.upper()
print(' '.join(wordlist))
guess = input('Guess your letter: ')
  
# if guess.upper() not in word
else:
count += 1
print('Incorrect!')
guess = input('Guess your letter: ')
  
# if wordlist all have letters, and no '_', then win;
if '_' not in wordlist:
print('You finally found the word.')
print('You totally use %d times' %count)
repeat = False

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

Python Code:

""" Python code that plays HangMan Game """

def display_word(secret_word, guessed_letters):
   """ Function that displays word guessed so far """
   for ch in secret_word:
       if ch in guessed_letters:
           print(ch, end="")
       else:
           print('-', end="")
   print()
          
def play_hangman():
   """ Plays hang man game """
   # Reading secret word from user
   sWord = "AABBCC";

   # Generating guess string
   gWord = ['-'] * len(sWord);

   # Variable that counts number of guesses
   guessCnt = 0

   # List that holds the guessed characters
   guessed = []
   display_word(sWord, guessed)
   # Iterating till user guess the correct word or number of trials reached
   while ('-' in gWord):
       # Reading guess from user
       guess = input('Guess a letter: ');
      
       # Validating character
       if not (guess.isalpha()) or len(guess) != 1:
           # Reading guess from user
           print('This is not a letter. Enter a letter.');
       else:
           # To upper case
           guess = guess.upper()
           # Checking guessed character
           if guess in guessed:
               print("You have already guessed " + guess)
           else:
               # Updating guessed character
               guessed.append(guess)
              
               # Checking for character
               if guess in sWord:
                   for i in range(len(sWord)):
                       # Checking character by character
                       if sWord[i].upper() == guess.upper():
                           gWord[i] = sWord[i];
               # Updating guess count
               guessCnt += 1;
           display_word(sWord, guessed)      
           # Checking for entire guess
           if '-' not in gWord:
               break;
              
   # Printing results      
   if '-' not in gWord:
       # Printing correct word
       print('You finally found the word.')
       print('You totally use %d times' %(guessCnt))      

def main():
   """ Main function """
   print("Welcome to hangman!")
   # Playing game
   play_hangman()
  
  
main()

_________________________________________________________________________________________________

Sample Run:

Add a comment
Know the answer?
Add Answer to:
It's a Python question, I got confused which I cannot get the same result as the...
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
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