By Mathissohardlmao


2019-10-09 14:38:50 8 Comments

import random
def main():
   num_guesses = 4


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

   list_of_words = ['apple', 'banana', 'watermelon', 'kiwi', 'pineapple', 'mango']
   answer=random.choice(list_of_words)
   puzzle=['_'] * len(answer)




   def display_instructions(instruction_file):
      file_contents=instruction_file.read()
      instruction_file=instruction_file.close()
      print(file_contents)





   def get_guess(num_guesses):
      print('The number of guesses remaining is ' + str(num_guesses)+ '.') 
      letter_input = input("Guess a letter ")
      return letter_input

   def update_puzzle_string(letter_input,puzzle,answer):
      if get_guess(num_guesses) in answer:
         for i,x in enumerate(answer):
            if x is get_guess:
               puzzle[i]=letter_input
               return True


   def display_puzzle_string(puzzle):
      print('The current state of the puzzle is '+str(puzzle))

   def is_word_found(puzzle,answer):
      is_word_found=True
      puzzle_string=print(''.join(puzzle))
      if puzzle_string == answer:
         return False





   def play_game(answer,puzzle):
      while True:
         display_puzzle_string(puzzle)
         get_guess(num_guesses)
         update_puzzle_string(get_guess,puzzle,answer)
         print(str(puzzle))



   is_word_found(puzzle,answer)
   display_instructions(instruction_file)         
   play_game(answer,puzzle)







main()

Sorry for formatting issues. The goal of this program is to collect a guess from a user then compare it to a randomly selected word from a list after doing so it updates a puzzle that has blanks where the letter belongs, if all letters of the word are guessed, user is told they are correct. User gets 4 guesses. It is basically hangman. When I execute this program it just prints the instructions, the initial puzzle state, requests a guess then keeps asking for guesses. I don't understand why this isn't working. I'll implement the number of guesses after I get help with this.

2 comments

@Teku1998 2019-10-09 14:53:25

import random
def main():
    num_guesses = 4
    instruction_file=open('instructions.txt', 'r')

    list_of_words = ['apple', 'banana', 'watermelon', 'kiwi', 'pineapple', 'mango']
    answer=random.choice(list_of_words)
    puzzle=['_'] * len(answer)

    def display_instructions(instruction_file):
        file_contents=instruction_file.read()
        instruction_file=instruction_file.close()
        print(file_contents)

    def get_guess(num_guesses):
        print('The number of guesses remaining is ' + str(num_guesses)+ '.') 
        letter_input = input("Guess a letter: ")
        return letter_input

    def update_puzzle_string(letter_input,puzzle,answer):
        for i,x in enumerate(answer):
            if x is letter_input:
                puzzle[i]=letter_input
        return

    def display_puzzle_string(puzzle):
        print('The current state of the puzzle is '+str(puzzle))

    def is_word_found(puzzle,answer):
        is_word_found=True
        puzzle_string=print(''.join(puzzle))
        if puzzle_string == answer:
            return False

    def play_game(answer,puzzle):
        while True:
            display_puzzle_string(puzzle) #display '_ _ _ _ _'
            guess = get_guess(num_guesses)
            update_puzzle_string(guess,puzzle,answer)
            #print(str(puzzle)) #this statement is causing the repetitive puzzle prints

    is_word_found(puzzle,answer)
    display_instructions(instruction_file)
    play_game(answer,puzzle)

main()

After fixing your formatting issues, the code now repeatedly asks for a guess and takes input. I think the issue mostly existed in formatting because I get no errors now. Also, did you have random imported before? Cheers

EDIT:

See changes to both update_puzzle_string() and play_game. You repeatedly called the get_guess function rather than using its initial return value. example (note that repeat letters are not supported under this code)

EDIT 2: (refer to comments of this answer)

See update_puzzle_string() for the change regarding "multiple of the same letter in the answer" program now places all of the right letter in place

@Mathissohardlmao 2019-10-09 14:55:25

Oh I'm sorry I meant that it keeps asking for a guess rather than updating the puzzle string with the guess. Like it just keeps asking over and over for a guess rather than proceeding to update puzzle or print the updated puzzle.

@Teku1998 2019-10-09 14:58:34

I will update, I believe I found it.

@Mathissohardlmao 2019-10-09 15:12:13

Thank you so much! I was so lost it was unbelievable, you're a life saver.

@Teku1998 2019-10-09 15:15:43

@Mathissohardlmao no problem, glad I could help! Thanks for accepting the answer :)

@Mathissohardlmao 2019-10-09 15:37:11

Unrelated to original question, but why does the guess not fill in all appropriate letters of the word. For ex. the guess e for watermelon only fills in one of the e's.

@Teku1998 2019-10-09 16:14:47

@Mathissohardlmao see my updated answer on that for the code change.

@Alexander Santos 2019-10-09 14:59:48

So your error was not being able to stop the software?

You can use is_word_found inside play_game and then break if found.

Like this:

def play_game(answer,puzzle):
    while True:
        display_puzzle_string(puzzle)
        get_guess(num_guesses)
        update_puzzle_string(get_guess,puzzle,answer)
        if is_word_found(puzzle, answer):
            print("You won!")
            break
        print(str(puzzle))

Break will stop while True infinite loop

@Teku1998 2019-10-09 16:24:11

His initial issue was a loop created by calling functions repeatedly instead of saving the result of the function into a variable and using that.

Related Questions

Sponsored Content

25 Answered Questions

[SOLVED] Set a default parameter value for a JavaScript function

17 Answered Questions

[SOLVED] How to make a chain of function decorators?

12 Answered Questions

[SOLVED] Calling a function of a module by using its name (a string)

  • 2008-08-06 03:36:08
  • ricree
  • 621163 View
  • 1538 Score
  • 12 Answer
  • Tags:   python object

62 Answered Questions

[SOLVED] Calling an external command in Python

31 Answered Questions

[SOLVED] What's the difference between a method and a function?

21 Answered Questions

[SOLVED] What is the difference between call and apply?

38 Answered Questions

[SOLVED] var functionName = function() {} vs function functionName() {}

18 Answered Questions

[SOLVED] Using global variables in a function

10 Answered Questions

[SOLVED] What does the exclamation mark do before the function?

  • 2010-09-20 21:21:51
  • Sebastian Otto
  • 153632 View
  • 1180 Score
  • 10 Answer
  • Tags:   javascript function

Sponsored Content