Skip to content
Open
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
27 changes: 26 additions & 1 deletion game.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,32 @@ def snowman(snowman_word):
If the player wins and,
'Sorry, you lose! The word was {snowman_word}' if the player loses
"""
pass
snowman_word = snowman_word.lower()
correct_letter_guess_statuses = build_letter_status_dict(snowman_word)
wrong_guesses_list = []

print_word_progress_string(snowman_word, correct_letter_guess_statuses)

while (
Copy link
Author

Choose a reason for hiding this comment

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

With this particular program, we have two potential finish conditions: 1. We find the word, 2. We use up all our guesses. One is a win condition and one is a lose condition.

There are a couple different ways we could handle these two conditions:

  1. Place both in a compound conditional within the while loop.
  2. Use one as the while loop conditional and nest the other conditional within the while loop.
    You have opted for the former, which Is good, but it does increase a bit of the work that needs to be done outside the loop. This happens for the following reasons:

If we check both for each iteration, our loop ends if either the win or lose condition is met. This means that we are going to have to make an additional and unnecessary check after the loop to see which condition was met. If we somehow resolved one win condition inside the loop, we would remove the need for extra checks after the while loop has complete.

If we meet one of the conditions inside the loop, we can exit the function early with an explicit return. This resolves one of our conditions that we no longer have to worry about later on.

Overall your approach works, but it does add a little bit of extra code!

len(wrong_guesses_list) < SNOWMAN_MAX_WRONG_GUESSES
and not is_word_guessed(snowman_word, correct_letter_guess_statuses)
):
user_input = get_letter_from_user(
correct_letter_guess_statuses, wrong_guesses_list
).lower()
Comment on lines +33 to +35
Copy link
Author

Choose a reason for hiding this comment

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

When it comes to writing Python code, one of the guides you'll see us reference is the PEP 8 Style Guide. This is a guide for best practices when it comes to styling our code. One of the most common things we see is to try and keep individual lines of code under 79 characters. You've noticed this and made a good choice by breaking it up across multiple lines! I would just clean it up just a bit!

Suggested change
user_input = get_letter_from_user(
correct_letter_guess_statuses, wrong_guesses_list
).lower()
user_input = get_letter_from_user(
correct_letter_guess_statuses,
wrong_guesses_list
).lower()

Ideally, if we have multiple parameters, put each on its own line!


if user_input in correct_letter_guess_statuses:
Copy link
Author

Choose a reason for hiding this comment

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

I just wanted to highlight a really cool thing that you did here! When we receive a letter from the user, we want to check to see if the letter exists within the word or not. Given the way this program is set up, we actually end up with two different collections that hold all the letters of the word, a string (snowman_word) and a dictionary (correct_letter_guess_statuses).

While we could check either one for the letter we've received, searching within a dictionary is ever so slightly more efficient than searching through a string (We'll talk more about why in Unit 1), so great choice here!

correct_letter_guess_statuses[user_input] = True
else:
wrong_guesses_list.append(user_input)

print_snowman_graphic(len(wrong_guesses_list))
print_word_progress_string(snowman_word, correct_letter_guess_statuses)
Comment on lines +42 to +43
Copy link
Author

Choose a reason for hiding this comment

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

Great choice to print both of these outside the conditionals!


if is_word_guessed(snowman_word, correct_letter_guess_statuses):
print("Congratulations, you win!")
else:
print(f"Sorry, you lose! The word was {snowman_word}")
Comment on lines +45 to +48
Copy link
Author

Choose a reason for hiding this comment

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

f we moved this conditional from where it currently is to inside the loop, we could handle one of the ending conditions inside the loop and remove it from the while conditionals. We could then exit the function early with an explicit return statement! That allows us only deal with the lose condition outside of the loop. In fact, we would know that the user has lost if the loop ends, so we could just include the print statement on line 48 outside the loop!



def print_snowman_graphic(wrong_guesses_count):
Expand Down