-
Notifications
You must be signed in to change notification settings - Fork 90
C24 Crow - Mengqiao Han #10
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 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -17,25 +17,56 @@ 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, | ||||||||||||
| If the player wins and, | ||||||||||||
| 'Sorry, you lose! The word was {snowman_word}' if the player loses | ||||||||||||
| """ | ||||||||||||
| pass | ||||||||||||
| wrong_guesses_list = [] | ||||||||||||
| wrong_guesses_count = len(wrong_guesses_list) | ||||||||||||
| correct_letter_guess_statuses = build_letter_status_dict(snowman_word) | ||||||||||||
| victory = False | ||||||||||||
| while wrong_guesses_count < SNOWMAN_MAX_WRONG_GUESSES and not victory: | ||||||||||||
|
Comment on lines
+26
to
+27
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. Nice work with this Consider adding a few blank lines within your function to separate logically distinct sections of the code (e.g., setup, processing, return). According to PEP 8, blank lines can improve readability by visually grouping related operations. Right now, everything runs together, which can make it harder to quickly understand the structure and flow of your function.
Suggested change
|
||||||||||||
| user_guess = get_letter_from_user(correct_letter_guess_statuses, wrong_guesses_list) | ||||||||||||
| if user_guess not in snowman_word: | ||||||||||||
|
Comment on lines
+28
to
+29
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.
Suggested change
Comment on lines
+28
to
+29
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. Note that the for letter in snowman_word:
if user_guess == letter:
return False
return TrueYou can use this syntax to perform the above logic over a multiple kinds of iterables/sequences in Python. Later in the curriculum we will learn that they don't all have the same performance (Big O). |
||||||||||||
| wrong_guesses_list.append(user_guess) | ||||||||||||
| wrong_guesses_count = len(wrong_guesses_list) | ||||||||||||
| print_snowman_graphic(wrong_guesses_count) | ||||||||||||
|
Comment on lines
+30
to
+32
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. Something that we could add here to enhance the user experience is adding a list of the wrong guesses each wrong guess. This would help the user not make multiple wrong guesses of the same letter. |
||||||||||||
|
|
||||||||||||
| else: | ||||||||||||
| correct_letter_guess_statuses[user_guess] = True | ||||||||||||
| print_word_progress_string(snowman_word, correct_letter_guess_statuses) | ||||||||||||
|
Comment on lines
+35
to
+36
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. ✅ |
||||||||||||
| victory = 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.
if is_word_guessed:
breakThis would remove the |
||||||||||||
| if victory: | ||||||||||||
| print("Congratulations, you win!") | ||||||||||||
| else: | ||||||||||||
| 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 | ||||||||||||
| """This function prints out the appropriate snowman image | ||||||||||||
| depending on the number of wrong guesses the player has made. | ||||||||||||
| """ | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| for i in range(SNOWMAN_MAX_WRONG_GUESSES - wrong_guesses_count, SNOWMAN_MAX_WRONG_GUESSES): | ||||||||||||
| print(SNOWMAN_GRAPHIC[i]) | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
|
|
||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| 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. | ||||||||||||
| """ | ||||||||||||
|
|
||||||||||||
| 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 | ||||||||||||
| """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 | ||||||||||||
| It asks for input from the user of a single character until | ||||||||||||
| a valid character is provided and then returns this character. | ||||||||||||
| """ | ||||||||||||
|
|
||||||||||||
|
|
@@ -48,33 +79,33 @@ def get_letter_from_user(correct_letter_guess_statuses, wrong_guesses_list): | |||||||||||
| 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!") | ||||||||||||
| else: | ||||||||||||
| valid_input = True | ||||||||||||
|
|
||||||||||||
| return user_input_string | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| 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 | ||||||||||||
| """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 | ||||||||||||
| It calls another function to generate a string representation of the | ||||||||||||
| user's progress towards guessing snowman_word and prints this string. | ||||||||||||
| """ | ||||||||||||
|
|
||||||||||||
|
|
@@ -85,8 +116,8 @@ def print_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 | ||||||||||||
| guess placements as well as the placements for the letters yet to be | ||||||||||||
| 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. | ||||||||||||
| """ | ||||||||||||
|
|
||||||||||||
|
|
@@ -116,4 +147,4 @@ def is_word_guessed(snowman_word, correct_letter_guess_statuses): | |||||||||||
| for letter in snowman_word: | ||||||||||||
| if not correct_letter_guess_statuses[letter]: | ||||||||||||
| return False | ||||||||||||
| return True | ||||||||||||
| return True | ||||||||||||
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.
Nice work on these naming conventions, they readily let other developers know what data they are referencing. I also like that you leveraged the
lenfunction withwrong_guesses_countto get the number of wrong guesses a user has submitted. Though note that it is not necessary to have this variable, we could just check thelenofwrong_guesses_listwhenever we need it.