Created
February 27, 2018 13:08
-
-
Save umair-tp/0f1a42a401c6425722dbfb609810de20 to your computer and use it in GitHub Desktop.
Exercise 32 : This exercise is Part 3 of 3 of the Hangman exercise series. The other exercises are: Part 1 and Part 2. You can start your Python journey anywhere, but to finish this exercise you will have to have finished Parts 1 and 2 or use the solutions (Part 1 and Part 2). In this exercise, we will finish building Hangman. In the game of Han…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import random, sys | |
| def displayHint(answers, choosed_word): | |
| won = True | |
| for i in range(len(choosed_word)-1): | |
| if choosed_word[i] in answers: | |
| print choosed_word[i], | |
| else: | |
| won = False | |
| print '-', | |
| print "" | |
| if won: | |
| print "You guessed it correct!" | |
| sys.exit() | |
| def main(): | |
| answers = [] | |
| with open('sowpods.txt') as dict_file: | |
| words_list = dict_file.read().split('\n') | |
| choosed_word = words_list[random.randint(0, len(words_list))] | |
| for i in range(6): | |
| displayHint(answers, choosed_word) | |
| choice = raw_input("Enter you word : ").upper() | |
| if not choice in answers: | |
| answers.append(choice) | |
| if (len(answers) == len(set(choosed_word))): | |
| print "You guessed it correct in " + str(i) + " tries" | |
| sys.exit() | |
| print "Sorry you ran out of tries count" | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment