-
Notifications
You must be signed in to change notification settings - Fork 0
Possum - Priscilla A. #21
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,13 +14,33 @@ | |
|
|
||
|
|
||
| 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 = [] | ||
| wrong_guesses_count = 0 | ||
|
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. This variable to track the number of wrong guesses isn't necessary. Any time we need to know the count, we can get the length of the wrong guesses list. While there is a small bit of efficiency gained by not needing to call So unless calculating a value from data we're already tracking has a high cost (we'll be talking more about this in future material), try to minimize introducing additional variables that duplicate information. 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. Thank you, I agree with you, but I could only change the snowman function body, and the other helper methods used the variable wrong_guesses_count to print the snowman, so I was forced to use it to pass the tests. "def print_snowman_graphic(wrong_guesses_count): " 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. this was my code before I needed to arrange for this: def snowman(): |
||
|
|
||
| while (len(wrong_guesses_list) < SNOWMAN_MAX_WRONG_GUESSES | ||
| and not is_word_guessed(snowman_word, correct_letter_guess_statuses)): | ||
|
Comment on lines
+21
to
+22
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 inclusion of both key checks for whether the game is still in progress. The game continues as long as the allowed number of wrong guesses haven't been used up, and the word hasn't yet been guessed. We had to be careful to express this with "as long as" phrasing, which is a tricky part of working with
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. Wrapping a long condition for readability can be tricky. PEP8 typically recommends indenting a wrapped line, but for a loop condition, this would align the wrapped line with the loop block. A common alternative is to over-indent the wrapped line (indent it two levels), so that the single indentation of the block is at a different level. I've often gone the route you did here of leaving a blank between the condition and the block, though this seems to be less common in the community, so I've tried to switch to the overindent method. Regardless of how we deal with the block, we should always indent a wrapped line at least one more level, so that it's clearly not a standalone line of code.
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. Alternatively, we could shorten this condition to focus solely on the number of guesses, and have a check within the loop for whether the word has been guessed. while len(wrong_guesses_list) < SNOWMAN_MAX_WRONG_GUESSES:
# later
if is_word_guessed(snowman_word, correct_letter_guess_statuses):
print("Congratulations, you win!")
print_word_progress_string(snowman_word, correct_letter_guess_statuses)
return |
||
|
|
||
| user_input_string = get_letter_from_user(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. Since we don't display the word status until after the guess, the player doesn't get to see the length of the word before guessing. This wasn't a requirement, but it would be nice to have from a gameplay perspective. |
||
| wrong_guesses_list) | ||
|
|
||
| if user_input_string 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. 👍 Nice decision to check whether the letter was in the word by using the status |
||
| print("\nYou guessed a letter that's in the word!\n" ) | ||
| correct_letter_guess_statuses[user_input_string] = True | ||
| else: | ||
| print(f"\nThe letter '{user_input_string}' is not in the word\n") | ||
| wrong_guesses_list.append(user_input_string) | ||
| wrong_guesses_count += 1 | ||
|
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 we removed the If you are going to use a helper variable to track the wrong guesses, then we could reduce some of the risk of dat adrift by updating it by reading the list |
||
|
|
||
| print(f"Wrong guesses: {wrong_guesses_list}\n") | ||
|
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. Python is able to print out a list of strings, which lets us show the wrong guesses to the player. However, we might consider writing our own function to customize the string we build (try looking into the |
||
| print_word_progress_string(snowman_word, correct_letter_guess_statuses) | ||
| print("\n") | ||
| print_snowman_graphic(wrong_guesses_count) | ||
|
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. This is the only place |
||
|
|
||
| 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. 👍 Our loop terminates either because the number of wrong guesses was used up, or because the word has been guessed. As a result, we do need to double check whether the game was won or lost. With this structure, it would be just as valid to check whether we were out of guesses, meaning the player lost, otherwise they won. Another alternative would be to drive the main loop using only one of the conditions (either there are still wrong guesses available, OR that the word hasn't yet been guessed). Then within the loop, we could check for the other condition and handle it directly. For example, if we ran the main loop based on the number of wrong guesses alone, we could have an |
||
| print("Congratulations, you win!") | ||
| else: | ||
| print(f"Sorry, you lose! The word was {snowman_word}") | ||
|
|
||
|
|
||
| def print_snowman_graphic(wrong_guesses_count): | ||
|
|
@@ -116,4 +136,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.
👍 These are the two pieces of data we need to make use of our other helper functions for tracking correct and incorrect guesses. Tracking the letter status involves using a dictionary with a particular structure, which our
build_letter_status_dicthelper is able to calculate from the random word.