Skip to content

Snowman project updates #6

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

Open
wants to merge 8 commits into
base: main
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
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ $ pip install -r requirements.txt
```

Summary of one-time project setup:
- [ ] Fork the project respository
- [ ] `cd` into your `projects` folder
- [ ] Clone the project onto your machine
- [ ] `cd` into the `snowman` folder
- [ ] Create the virtual environment `venv`
- [ ] Activate the virtual environment `venv`
- [ ] Install the dependencies with `pip`
- [x] Fork the project respository
- [x] `cd` into your `projects` folder
- [x] Clone the project onto your machine
- [x] `cd` into the `snowman` folder
- [x] Create the virtual environment `venv`
- [x] Activate the virtual environment `venv`
- [x] Install the dependencies with `pip`

# Running the Project

Expand All @@ -86,4 +86,4 @@ You will be prompted to enter "p" to play the game, or "t" to run the tests.

When you start, all of the tests will fail, and playing the game will exit the program immediately. Your goal is to make the tests pass and the game work!

To do this, you will need to complete the implementation of the `snowman` function in the file `game.py`. There are a number of other functions that we developed in the Precourse materials that are also included in that file for you to use.
To do this, you will need to complete the implementation of the `snowman` function in the file `game.py`. There are a number of other functions that we developed in the Precourse materials that are also included in that file for you to use.
35 changes: 28 additions & 7 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,37 @@
'-----------'
]


def snowman(snowman_word):
"""Complete the snowman function
replace "pass" below with your own code
It should print 'Congratulations, you win!'
If the player wins and,
'Sorry, you lose! The word was {snowman_word}' if the player loses
"""
pass

correct_letter_guess_statuses = build_letter_status_dict(snowman_word)
wrong_guesses_list = []
num_wrong_guesses = len(wrong_guesses_list)
is_winner = False

while num_wrong_guesses < SNOWMAN_MAX_WRONG_GUESSES and not is_winner:
user_input = get_letter_from_user(correct_letter_guess_statuses, wrong_guesses_list)

if user_input in snowman_word:
correct_letter_guess_statuses[user_input] = True
else:
wrong_guesses_list.append(user_input)
num_wrong_guesses += 1

if is_word_guessed(snowman_word, correct_letter_guess_statuses):
print('Congratulations, you win!')
is_winner = True

print_word_progress_string(snowman_word, correct_letter_guess_statuses)
print_snowman_graphic(num_wrong_guesses)
print(f"Incorrect letters: {', '.join(wrong_guesses_list)}")

if not is_winner:
print(f'Sorry, you lose! The word was {snowman_word}')

def print_snowman_graphic(wrong_guesses_count):
"""This function prints out the appropriate snowman image
Expand All @@ -44,12 +65,12 @@ def get_letter_from_user(correct_letter_guess_statuses, wrong_guesses_list):

while not valid_input:
user_input_string = input("Guess a letter: ")

if not user_input_string.isalpha():
print("You must input a letter!")
elif len(user_input_string) > 1:
print("You can only input one letter at a time!")
elif (user_input_string in correct_letter_guess_statuses
and correct_letter_guess_statuses[user_input_string]):
elif (user_input_string in correct_letter_guess_statuses and correct_letter_guess_statuses[user_input_string]):
print("You already guessed that letter and it's in the word!")
elif user_input_string in wrong_guesses_list:
print("You already guessed that letter and it's not in the word!")
Expand All @@ -64,7 +85,6 @@ def build_letter_status_dict(snowman_word):
a dictionary with a key-value pair for each letter in
snowman_word where the key is the letter and the value is `False`.
"""

letter_status_dict = {}
for letter in snowman_word:
letter_status_dict[letter] = False
Expand All @@ -82,7 +102,8 @@ def print_word_progress_string(snowman_word, correct_letter_guess_statuses):
print(progress_string)


def generate_word_progress_string(snowman_word, correct_letter_guess_statuses):
def generate_word_progress_string(snowman_word,
correct_letter_guess_statuses):
"""
This function takes the snowman_word and snowman_word_dict as input.
It creates and returns an output string that shows the correct letter
Expand Down
3 changes: 1 addition & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
if user_input == "p":
from game import snowman, SNOWMAN_MIN_WORD_LENGTH, SNOWMAN_MAX_WORD_LENGTH
from wonderwords import RandomWord

random_word_generator = RandomWord()
random_word_generator = RandomWord()
snowman_word = random_word_generator.word(word_min_length=SNOWMAN_MIN_WORD_LENGTH, word_max_length=SNOWMAN_MAX_WORD_LENGTH)

snowman(snowman_word)
Expand Down