Skip to content
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

Merge pull request #1 from tiyd-python-2015-01/master #14

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
103 changes: 103 additions & 0 deletions mystery_word.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import random
import re

with open("/usr/share/dict/words") as w:
word_list = w.readlines()

guessed = []
revealed = []

Copy link
Contributor

Choose a reason for hiding this comment

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

This stuff should be at the bottom in a if __name__ == "__main__" block, along with the rest of the code in the bottom of this file.


def random_word_assignment():
global random_word
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are you setting random_word to be global?

random_word = random.choice(word_list).strip().lower()
return random_word


def game_intro(random_word):
print_statement = "The word has {} letters. You have 8 guesses.".format(
len(random_word), counter)
print(print_statement)
return print_statement


def change_count_guess(counter):
counter -= 1
guessed.append(guess)
print(counter_statement)
return counter


# def display_counter():
# print(counter_statement)


def correct_guess(guess, revealed):
revealed.append(guess)
print("Good job! " + counter_statement)
return revealed


def incorrect_guess(guess, counter):
counter -= 1
guessed.append(guess)
print("Nope. " + counter_statement)
return guessed


def display_correct_guesses(guess):
print(" ".join(char if char in revealed else "_" for char in random_word))
return (" ".join(char if char in revealed else "_" for char
in random_word))
Copy link
Contributor

Choose a reason for hiding this comment

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

Repeated code breaks so easily -- if you're going to do this, save the result in a variable and then print and return it.



def get_guess():
guess = input("What's your guess? ").lower()
if guess in re.search[a-z]:
return str(guess)
print(guess)
Copy link
Contributor

Choose a reason for hiding this comment

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

This print statement will never get called b/c the return above it.

else:
print("{} is not a valid choice. Try again.".format(guess))


def guess_list():
print(guessed)
return guessed


# def game_over():
# game_over = "The word was {}."format(random_word)
# print(game_over)



counter = 8
game_intro(random_word_assignment())

while True:
while counter > 0 and len(random_word) != len(revealed):
guess = input("What's your guess? ")
counter_statement = "You have {} guesses left. '\n'".format(counter)
if len(guess) > 1:
guess = input("You can only guess one letter at a time. ")
elif guess in random_word:
correct_guess(guess, revealed)
display_correct_guesses(guess)
elif guess not in random_word:
counter -= 1
incorrect_guess(guess, counter)
display_correct_guesses(guess)
print(guessed)
else:
print("You have run out of guesses. The word is {}.".
format(random_word))
break

else:
if len(random_word) == len(revealed):
print("You won!")
break
else:
break

# Still accepts non-letters as correct. Replay function not working.
8 changes: 8 additions & 0 deletions mystery_word_game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import random

with open("/usr/share/dict/words") as w:
word_list = w.readlines()

def mystery_word():
mystery_word = random(word_list)
print("The word has {} letters.".format(len(mystery_word)))
113 changes: 113 additions & 0 deletions mystery_word_tdd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import random
import re

with open("/usr/share/dict/words") as w:
word_list = w.readlines()

guessed = []
revealed = []


def random_word_assignment():
global random_word
random_word = random.choice(word_list).strip().lower()
return random_word


def game_intro(random_word):
print_statement = "The word has {} letters. You have 8 guesses.".format(
len(random_word), counter)
print(print_statement)
return print_statement


def change_count_guess(counter):
counter -= 1
guessed.append(guess)
print(counter_statement)
return counter


# def display_counter():
# print(counter_statement)


def correct_guess(guess, revealed):
revealed.append(guess)
print("Good job! " + counter_statement)
return revealed


def incorrect_guess(guess, counter):
counter -= 1
guessed.append(guess)
print("Nope. " + counter_statement)
return guessed


def display_correct_guesses(guess):
print(" ".join(char if char in revealed else "_" for char in random_word))
return (" ".join(char if char in revealed else "_" for char
in random_word))


def get_guess():
guess = input("What's your guess? ").lower()
if guess in re.search[a-z]:
return str(guess)
print(guess)
else:
print("{} is not a valid choice. Try again.".format(guess))


def guess_list():
print(guessed)
return guessed


def game_over():
play_again = input("You won! Do you want to play again? Y or N ")
if play_again == 'y':
game_intro()
counter = 8
else:
print("goodbye.")


counter = 8
game_intro(random_word_assignment())

while True:
while counter > 0 and len(random_word) != len(revealed):
guess = input("What's your guess? ")
counter_statement = "You have {} guesses left. '\n'".format(counter)
if len(guess) > 1:
guess = input("You can only guess one letter at a time.
Try again. ")
elif guess in random_word:
correct_guess(guess, revealed)
display_correct_guesses(guess)
elif guess not in random_word:
counter -= 1
incorrect_guess(guess, counter)
display_correct_guesses(guess)
print(guessed)
else:
game_over()
print("You have run out of guesses. The word is {}.".
format(random_word))
break

else:
if len(random_word) == len(revealed):
play_again = input("You won! Do you want to play again? Y or N ")
if play_again == 'y':
game_intro()
counter = 8
else:
break
else:
game_over()
break

# Still accepts non-letters as correct. Replay function not working.
27 changes: 27 additions & 0 deletions test_mystery_word.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#Test each step

import mystery_word_tdd as mw

def test_random_word_assignment():
assert type(mw.random_word_assignment()) == str
Copy link
Contributor

Choose a reason for hiding this comment

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

Tests for type are not particularly useful. I'd drop this test.


def test_game_intro():
assert mw.game_intro("word", 8) == "The word has 4 letters. You have 8 guesses."

def test_change_count_guess():
assert mw.change_count_guess(8) == 7

def test_correct_guess():
assert (mw.correct_guess(guess)) == ["guess"]

def test_display_correct_guesses():
assert type(mw.display_correct_guesses("guess")) == str

# def test_get_guess():
# assert mw.get_guess() == "input"

def test_game_over():
assert mw.game_over() == "You lose. The word was " + random_word +".'"

def test_guess_list():
assert type(mw.guess_list) == list