diff --git a/README.md b/README.md index 337fd00..793ee95 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. \ No newline at end of file +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. diff --git a/game.py b/game.py index 493a74a..a5ce6ef 100644 --- a/game.py +++ b/game.py @@ -12,7 +12,6 @@ '-----------' ] - def snowman(snowman_word): """Complete the snowman function replace "pass" below with your own code @@ -20,8 +19,30 @@ def snowman(snowman_word): 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 @@ -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!") @@ -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 @@ -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 diff --git a/main.py b/main.py index c60c994..a1e36a3 100644 --- a/main.py +++ b/main.py @@ -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)