if statement - Hangman Python Repeats Fucntions -
when try run code keeps repeating,"so far have guessed" when want repeat once. output name when guess made should : secret_word = hello , after every word should h???o or h?ll?
def get_secret_word(): while true: secret_word = input('please enter word guessed\nthat not contain ? or whitespace:') if not ('?' in secret_word or ' ' in secret_word or secret_word == ''): return secret_word def is_game_over(wrong_guess, secret_word, output, chars_guessed): if wrong_guess == 7: print('you failed guess secret word:', secret_word) return true guess in secret_word: if guess in chars_guessed: output += guess else: output += '?' if not ('?' in output): print('you correctly guessed secret word:', secret_word) return true else: return false def display_hangman(wrong_guess): if wrong_guess == 1: print('\n |') elif wrong_guess == 2: print('\n |', '\n o') elif wrong_guess == 3: print('\n |', '\n o', '\n |') elif wrong_guess == 4: print('\n |', '\n o', '\n/|') elif wrong_guess == 5: print('\n |', '\n o', '\n/|\\') elif wrong_guess == 6: print('\n |', '\n o', '\n/|\\', '\n/') elif wrong_guess == 7: print('\n |', '\n o', '\n/|\\', '\n/', '\\') def display_guess(secret_word, chars_guessed): output = '' guess in secret_word: if guess in chars_guessed: output += guess else: output += '\nso far have guessed: ' guess in chars_guessed: output += guess + "," print(output.strip(",")) def get_guess(secret_word, chars_guessed): while true: guess_character = input('please enter next guess: ') if guess_character == '': print('you must enter guess.') continue elif len(guess_character) > 1: print('you can guess single character.') elif guess_character in chars_guessed: print('you guessed character:', guess_character) else: return guess_character def main(): wrong_guess = 0 chars_guessed = [] secret_word = get_secret_word() output = '' while not is_game_over(wrong_guess, secret_word, output, chars_guessed): display_hangman(wrong_guess) display_guess(secret_word, chars_guessed) guess_character = get_guess(secret_word, chars_guessed) chars_guessed.append(guess_character) chars_guessed.sort() if not guess_character in secret_word: wrong_guess += 1 return wrong_guess pass if __name__ == '__main__': main()
so issues can see are:
indentation issues(as pointed out of other posters already):
- get_secret_word() function if statement
- display_hangman() function elif's
also below bit of code main function
if not guess_character in secret_word: wrong_guess += 1 return wrong_guess pass
this exits out of app me , far can see return , pass lines not needed. return exits main function when incorrect guess made , pass never reached , since wrong_guess "global" variable in main function new count available in function length of time function active.
as repeated looping displaying users guesses way in how looping in display_guess()
function. in case character being guessed not in secret word loops on twice, once outer loop , again inner loop.
i think should change display_guess() below. way user see have guessed
def display_guess(): output = "\nso far have guessed: " count = 0 if len(chars_guessed) > 0: guess in chars_guessed: if count < len(chars_guessed) , count != 0: output += ',' output += guess count +=1 print output
as displaying secret word guessed characters , rest ?'s, doesn't seem printing value of output variable is_game_over() function anywhere. haven't tested adding print after loop should sort out.
on side note might wish add validation on user input excepts numbers , special characters
Comments
Post a Comment