-
Notifications
You must be signed in to change notification settings - Fork 89
Snowman PR - Maya Zakharova #7
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
base: main
Are you sure you want to change the base?
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 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -14,31 +14,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 | ||||||||||||
| print(f"random word: {snowman_word}") | ||||||||||||
|
|
||||||||||||
| correct_letter_guess_statuses = build_letter_status_dict(snowman_word) | ||||||||||||
| wrong_guesses_list = [] | ||||||||||||
|
|
||||||||||||
| def print_snowman_graphic(wrong_guesses_count): | ||||||||||||
| """This function prints out the appropriate snowman image | ||||||||||||
| depending on the number of wrong guesses the player has made. | ||||||||||||
| """ | ||||||||||||
| while len(wrong_guesses_list) < SNOWMAN_MAX_WRONG_GUESSES: | ||||||||||||
| user_input = get_letter_from_user(correct_letter_guess_statuses, wrong_guesses_list) | ||||||||||||
|
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. 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. This line currently goes past that limit! It won't cause the code to break or anything, but it is a good thing to keep in mind in terms of readability and best practices! I currently see two possible fixes here:
|
||||||||||||
|
|
||||||||||||
| if user_input in correct_letter_guess_statuses: | ||||||||||||
|
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. 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 it exists within the word or not. Given the way this program is set up, we end up with two different collections that hold all the letters of the word, a string ( 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! |
||||||||||||
| print("You guessed a letter that's in the word!") | ||||||||||||
| correct_letter_guess_statuses[user_input] = True | ||||||||||||
| else: | ||||||||||||
| print(f"The letter {user_input} is not in the word") | ||||||||||||
| wrong_guesses_list.append(user_input) | ||||||||||||
|
|
||||||||||||
| print_snowman_graphic(len(wrong_guesses_list)) | ||||||||||||
| print_word_progress_string(snowman_word, correct_letter_guess_statuses) | ||||||||||||
| print(f"Wrong guesses: {wrong_guesses_list}") | ||||||||||||
|
Comment on lines
+32
to
+34
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. Great job printing these items out after you've determined whether the chosen letter exists in the word or not! |
||||||||||||
|
|
||||||||||||
| if is_word_guessed(snowman_word, correct_letter_guess_statuses): | ||||||||||||
|
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. 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:
You have opted for the latter, which I think makes sense for a few reasons:
|
||||||||||||
| print("Congratulations, you win!") | ||||||||||||
| return | ||||||||||||
|
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. Great idea to use a return statement here! This will stop the function early as soon as we find the correct word. This is a great approach to use where possible! One small tweak to make lies in the difference between an implicit and explicit return statement. If we don't include the Both are valid ways to exit a function, but best practices ask us to remain consistent in which one we use within a function. While you have an explicit return here, there is technically an implicit return after line 40. The explicit return here is necessary, so it would be best practice to include a return statement under line 40 as well! |
||||||||||||
|
|
||||||||||||
| print(f"Sorry, you lose! The word was {snowman_word}") | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def print_snowman_graphic(wrong_guesses_count): | ||||||||||||
| for i in range(SNOWMAN_MAX_WRONG_GUESSES - wrong_guesses_count, SNOWMAN_MAX_WRONG_GUESSES): | ||||||||||||
| print(SNOWMAN_GRAPHIC[i]) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def get_letter_from_user(correct_letter_guess_statuses, wrong_guesses_list): | ||||||||||||
| """This function takes the snowman_word_dict and the list of characters | ||||||||||||
| that have been guessed incorrectly (wrong_guesses_list) as input. | ||||||||||||
| It asks for input from the user of a single character until | ||||||||||||
| a valid character is provided and then returns this character. | ||||||||||||
| """ | ||||||||||||
|
Comment on lines
-36
to
-40
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. Docstrings are often a great way to explain what the following function does and how it operates. It is very common practice to see docstrings at the start of every function. If that is the case, no need to remove them! Go ahead and keep them in, especially if they exist in both finished and unfinished functions. This is a good indicator that the programmer intended for them to stay! |
||||||||||||
|
|
||||||||||||
| valid_input = False | ||||||||||||
| user_input_string = None | ||||||||||||
|
|
||||||||||||
|
|
@@ -60,36 +66,18 @@ def get_letter_from_user(correct_letter_guess_statuses, wrong_guesses_list): | |||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def build_letter_status_dict(snowman_word): | ||||||||||||
| """This function takes snowman_word as input and returns | ||||||||||||
| 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 | ||||||||||||
| return letter_status_dict | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def print_word_progress_string(snowman_word, correct_letter_guess_statuses): | ||||||||||||
| """ | ||||||||||||
| This function takes the snowman_word and snowman_word_dict as input. | ||||||||||||
| It calls another function to generate a string representation of the | ||||||||||||
| user's progress towards guessing snowman_word and prints this string. | ||||||||||||
| """ | ||||||||||||
|
|
||||||||||||
| progress_string = generate_word_progress_string(snowman_word, correct_letter_guess_statuses) | ||||||||||||
| print(progress_string) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| 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 | ||||||||||||
| guess placements as well as the placements for the letters yet to be | ||||||||||||
| guessed. | ||||||||||||
| """ | ||||||||||||
|
|
||||||||||||
| output_string = "" | ||||||||||||
| is_not_first_letter = False | ||||||||||||
|
|
||||||||||||
|
|
@@ -108,11 +96,6 @@ def generate_word_progress_string(snowman_word, correct_letter_guess_statuses): | |||||||||||
|
|
||||||||||||
|
|
||||||||||||
| def is_word_guessed(snowman_word, correct_letter_guess_statuses): | ||||||||||||
| """ | ||||||||||||
| This function takes the snowman_word and snowman_word_dict as input. | ||||||||||||
| It returns True if all the letters of the word have been guessed, and False otherwise. | ||||||||||||
| """ | ||||||||||||
|
|
||||||||||||
| for letter in snowman_word: | ||||||||||||
| if not correct_letter_guess_statuses[letter]: | ||||||||||||
| return False | ||||||||||||
|
|
||||||||||||
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.
While we are writing code, print statements like this can be a great way to debug! They don't need to make it into the final submission however, so feel free to take them out before submitting!