-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[WIP] Implement hangaman excercise #905
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| # Hangman | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import random | ||
|
|
||
|
|
||
| def main(): | ||
| while True: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like this infinite loop
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @m-a-ge This loop is only if player wan't to play again |
||
| words = ["python", "golang", "java", "assembly", "haskel", "emoji"] | ||
| chosen_word = random.choice(words).lower() | ||
| player_guess = None | ||
| guessed_letters = [] | ||
| word_guessed = [] | ||
| for letter in chosen_word: | ||
| word_guessed.append("*") | ||
| joined_word = None | ||
| chances = 10 | ||
| attempts = chances - 1 | ||
| while (attempts != 0 and "-" in word_guessed): | ||
| print(("\nYou have {} attempts remaining").format(attempts)) | ||
| joined_word = "".join(word_guessed) | ||
| print(joined_word) | ||
|
|
||
| try: | ||
| player_guess = str( | ||
| input("\nPlease select a letter between A-Z" + "\n> ")).lower() | ||
| except: | ||
| print("please enter valid input and try again") | ||
| continue | ||
| else: | ||
| if not player_guess.isalpha(): | ||
| print("That is not a letter. Please try again.") | ||
| continue | ||
| elif len(player_guess) > 1: | ||
| print("That is more than one letter. Please try again.") | ||
| continue | ||
| elif player_guess in guessed_letters: | ||
| print("You have already guessed that letter. Please try again.") | ||
| continue | ||
|
|
||
| guessed_letters.append(player_guess) | ||
|
|
||
| for pos, letter in enumerate(chosen_word): | ||
| if player_guess == letter: | ||
| word_guessed[pos] = player_guess | ||
|
|
||
| if player_guess not in chosen_word: | ||
| attempts -= 1 | ||
|
|
||
| if "-" not in word_guessed: | ||
| print(("\nCongratulations! {} was the word").format(chosen_word)) | ||
| else: | ||
| print(("\nUnlucky! The word was {}.").format(chosen_word)) | ||
|
|
||
| print("\nWould you like to play again?") | ||
|
|
||
| response = input("> ").lower() | ||
| if response not in ("yes", "y"): | ||
| break | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import random | ||
|
|
||
|
|
||
| def main(): | ||
| pass | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import unittest | ||
| import hangman | ||
|
|
||
|
|
||
| class HangmanTest(unittest.TestCase): | ||
| def test(self): | ||
| self.assertEqual("hangman", "hangman") | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More tests should be added
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vaibhavsingh97, I would suggest basing the tests on the ones from the C# track since they have fairly good test coverage. |
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can adopt
README.mdfrom here - https://github.com/exercism/problem-specifications/blob/master/exercises/hangman/description.md