Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions exercises/hangman/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Hangman
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

60 changes: 60 additions & 0 deletions exercises/hangman/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import random


def main():
while True:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this infinite loop

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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()
9 changes: 9 additions & 0 deletions exercises/hangman/hangman.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import random


def main():
pass


if __name__ == "__main__":
main()
11 changes: 11 additions & 0 deletions exercises/hangman/hangman_test.py
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")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More tests should be added

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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()