View the step-by-step solution to:

Question

hello, I am having issues with the win condition of the game where if the player has guessed the correct word, it

will not display the win message without pressing enter one more time. It also does not display the win message if the player guess all the correct letters than then starts to guess incorrect letters. I am guessing it is because of the line

if guess in wordchoice:

guessbank[guess] = 1



from random import randint


def main():

  list_of_words = ['apple', 'banana', 'watermelon', 'kiwi', 'pineapple', 'mango']

   

  # display instructions

  def instruction():

   f = open('instructions.txt', 'r')  

    instructions = f.read()

    print(instructions)

    f.close() 

  instruction()

   

  # random word generation

  words = len(list_of_words)-1

  wordchoice = list_of_words[randint(0,words)] 

   

  # calculating length, prepare initial settings of game

  length = len(wordchoice)

  guessbank = dict.fromkeys(wordchoice, 0)

  print('The answer so far is' , '_ '*length)

  progress = 0

  i = 0

  life = 4

  # loop for # of attempts

  while i < 4 and progress < length:

  # game result

    def iswin(): 

      if progress == length:

        print("Good Job! You found the word",wordchoice)

             

      if progress != length and life == 0:   

        print("Not quite, the correct word was" , wordchoice)       

    guess = input("Guess a letter ("+str(life)+' guesses remaining):')

     

    # Answer check/score and guess tally

    if guess in wordchoice:

      guessbank[guess] = 1

      progress += 1

    else:

      i = i+1

      life=life-1

     

    #display answer/hide missing letters

    print("The answer so far is")

    print(" ".join([alp if guessbank[alp] else "_" for alp in wordchoice]))       

        

    iswin()

   

  # End game

  end = input("Press enter to end the game.")

main()

Recently Asked Questions

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question
Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes