infinite loop in Hangman game in Python -


i trying build simple hangman game, following program creating infinite loop when letter entered user not part of word guess (it's printing "*" indefinitely). missing here? advice appreciated.

import re import random  folder = open("datas.txt","r") data = folder.read()  word_list = re.sub("[^\w]"," ", data).split() chosen_word = random.choice(word_list)  letter_player = input('enter letter pls:\n')  continue_game = false  masked_word = [] letter in chosen_word: masked_word.append("*")  found_letters = []  def guess_letter(): letter in range(0,len(chosen_word)):     if letter_player == chosen_word[letter]:         found_letters.append(letter_player)         masked_word[letter] = letter_player      else:         masked_word[letter] = '*'         print(masked_word[letter])  return found_letters  str_found_letters = ''.join(found_letters) print(str_found_letters)  if(str_found_letters != chosen_word):     continue_game = true  while continue_game:    guess_letter() 

as there others answering, why not hand out try, op continue? here version fixing overwrite error of previous found letters in subsequent iterations.note also, append of letters found in list may 1 wants, or maybe not, o in foo appended twice.

# python 3/2, imports , literal:

from __future__ import print_function import re import random  mask_char = '*' 

# read words file , select randomly:

def read_game_data(source="datas.txt"):     """randomly select word source data."""      open(source, "r") f_data:         return random.choice(re.sub("[^\w]", " ", f_data.read()).split()) 

# build sequences (here lists) of letters constitute word or "mask"

def build_word_seq(a_word, a_mask=none):     return [letter if not a_mask else a_mask letter in a_word] 

# core evaluation function (previously named guess_letter)

def evaluate_letter(chosen_word, masked_word, letter_player, found=none):     if found none:         found = set()     pos in range(len(chosen_word)):         if letter_player == chosen_word[pos]:             found.add(letter_player)             masked_word[pos] = letter_player     return masked_word, found 

# function replaces continue_game variable:

def not_ready(chosen_word, masked_word):     """evaluate."""     return true if masked_word != chosen_word else false 

# put business in main function, minimize globals:

def main():     """do game."""     chosen_word = build_word_seq(read_game_data())     mask = build_word_seq(chosen_word, a_mask=mask_char)     found_letters = set()     while not_ready(chosen_word, mask):         letter_player = input('enter letter pls:\n')         mask, found_letters = evaluate_letter(             chosen_word, mask, letter_player, found_letters)         print(''.join(mask))         if found_letters:             print("lettersfound: %s" % (sorted(found_letters),))   if __name__ == '__main__':     main() 

a typical run based on datas.txt file with:

foo bar baz yes no 

yields e.g:

$ python3 hangman_again_inf_loop.py  enter letter pls: f *** enter letter pls: y *** enter letter pls: b b** lettersfound: ['b'] enter letter pls: ba* lettersfound: ['a', 'b'] enter letter pls: r bar lettersfound: ['a', 'b', 'r'] 

the loop again may infinite (if not guess right ;-)

for hangman game, there should equivalent counting logic, guessing competes against line line drawn hangman ...

above code still needs replacement of input() raw_input() when python v2 being used ... op used print() without future import, plausible, python v3 solution nuff.

happy hacking!


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -