Youre going to build this program in parts. In this exercise, you will build a program that
Question:
You’re going to build this program in parts. In this exercise, you will build a program that stores a secret word and allows the user to repeatedly guess a letter. Each time the user makes a guess, your program will report whether or not that letter is in the secret word.
End Result
An interaction with your final program for Part 1 might look like this:
Guess: asdf Your guess must have exactly one character! Guess: a That letter is not in the secret word! Guess: e That letter is in the secret word! Guess: 1 Your guess must be a lowercase letter! Guess: 123 Your guess must have exactly one character! Guess: t That letter is in the secret word!
(You’ll have to click the “Stop” button to stop your program… otherwise it will go on forever!)
Step 1 - Secret Word Variable
Make a string variable, secret_word, that stores the secret word.
Step 2 - Retrieve User’s Guess
Make a function, get_guess(), that retrieves a guess from the user and returns it as a string. This function should repeatedly ask the user for a guess until they enter something that is (1) exactly one character long, and (2) a lowercase letter. You will probably use a while loop, if statements, and a break statement to accomplish this. You will also need to use the string method islower(), which only returns True if the string you call it on consists only of lowercase letters. You can do something like this:
if not my_string.islower(): ... tell the user they need to enter a lowercase letter ...
Calling this function once might result in the following console interaction:
Guess: 123 Your guess must have exactly one character! Guess: 1 Your guess must be a lowercase letter! Guess: A Your guess must be a lowercase letter! Guess: a
In the above example, the function would return the string "a".
Step 3 - Put It All Together
Put calls to this function in a while loop, so that you repeatedly retrieve a guess from the user and determine whether or not it is in your secret word.
Here is the code I've written for this part. It's close, but when it prints one one character it's still running the print statement saying I should have only one character. That probably means there's something wrong with my elif statement.
secret_word = ("chocolate")
dashes = ("---------")
my_string = ""
def get_guess(my_string):
while len(my_string) != 1:
my_string = input("Guess: ")
if not my_string.islower():
return ("Your guess must be a lowercase letter!")
elif my_string.find != [0]:
return ("Your guess must be exactly one character!")
else:
return my_string
print get_guess(my_string)
PART 2
In this exercise, you should start with your solution code for Guess the Word, Part 1.
At this point, you should have a program that stores a secret word and correctly reports whether or not a user’s guess is in the word.
End Result
When you are finished with this part of the project, you’ll have a program that can print a string with dashes and letters in the correct places based on the user’s guesses, like this:
-------- Guess: e That letter is in the word! e------- Guess: t That letter is not in the word. e------- Guess: g That letter is in the word! egg----- Guess:
Step 1 - Variable For Dashes
Make another string variable called dashes that holds a number of dashes equal to the length of the secret word. So, if your secret word is “eggplant”, your dashes variable should have eight dashes.
Before printing your prompt that says “Guess: “, print the value of your dashes variable.
Step 2 - Update the Dashes
Here’s the tricky part. Write a function called update_dashes that takes three string arguments - the secret word, the current state of dashes, and the most recent guess - and returns the new state of dashes.
So, for example, if the word is "eggplant", the current value of dashes is "e-------", and the guess is "g", the function should return "egg-----".
If the word is "eggplant", the current value of dashes is "e-------", and the guess is "z", the function should return "e-------" (nothing changes, since the guess was incorrect).
Here’s how you might go about this.
In your function, start with an empty result string. Write a for loop from 0 up to but not including the length of your secret word. Say you use the variable i in your for loop. Compare the character in secret_word at index i to the guess. If they’re equal, then that means the guess matches that letter in the word, so you should add the guess to the result. Otherwise, the guess does not match that letter in the word, so you should add whatever was at index i in dashes to your result.
Wait, why don’t I just add a dash in the second case?
If you just add a dash whenever the guess doesn’t match the character in secret_word at index i, that will wipe out any correct guesses the user has already made! Imagine the case where the word is “eggplant”, the state of dashes is “e——-“, and the guess is “g”. If you always add a dash when the guess doesn’t match the character in secret_word at index i, the result would be “-gg—–”. Suddenly, the “e” is gone, because “g” did not match “e”! By instead using dashes at index i, you might append either a letter or a dash, depending on whether or not the user had already guessed that letter prior to the current guess.
Once your for loop is done, your result string should have letters and dashes in all the right places, so you can just return it!
Step 3 - Put It All Together
Whenever the user guesses correctly, use your update_dashes function to actually update your variable called dashes.
Now, each time you print your dashes variable, it should reflect what the user has and has not guessed!
Ok, what’s left?
At this point, your program doesn’t report when the user has guessed the whole word, nor does it penalize the user for incorrect guesses. That’s what you’ll build in the next part!
You also have the user guess the same word each time. This will probably get pretty boring after the first attempt. In the final part, you’ll change your program to select a random word from a list of words.
PART 3
In this exercise, you should start with your solution code for Guess the Word, Part 2.
At this point, you should have a program that can print a string with dashes and letters in the correct places based on the user’s guesses.
End Result
When you’re finished with this exercise, you’ll have a program that more or less correctly simulates the full game. The person running your program can win by guessing all the letters in the word, and they can lose by running out of guesses!
Step 1 - Letting the User Win
Right now, you have an infinite loop that lets the user enter guesses until they click the “Stop” button. Change the loop so that it actually has a condition that it checks. That condition should be False if the user guessed every letter, and True otherwise. (How might you write a boolean expression that does this?)
Outside the while loop, you should print something that says, “Congrats! You win. The word was: “, followed by the secret word. You can do this because you know that if the while loop finished, the condition became False, which means the user guessed every letter.
Step 2 - Letting the User Lose
Make a variable called guesses_left that stores an integer corresponding to how many incorrect guesses the user has left. Set it to 10 initially. Each time the user guesses a letter that is not in the word, subtract one from this variable.
Print the value of this variable each time you prompt the user for a guess, like this:
-------- 10 incorrect guesses left. Guess: e That letter is in the word! e------- 10 incorrect guesses left. Guess: t That letter is not in the word. e------- 9 incorrect guesses left. Guess: g That letter is in the word! egg----- 9 incorrect guesses left. Guess: r That letter is not in the word. egg----- 8 incorrect guesses left. Guess:
Modify the condition in the while loop so that it only returns True if:
- The user hasn’t guessed every letter yet
- The user hasn’t run out of guesses.
Now, after the while loop, you can’t be sure whether the user won or lost - you just know that one of those two things happened! Modify this part of your program so that instead of blindly printing a congratulatory message, you use an if statement to print the appropriate message depending on whether the user won or lost. If the user loses, you should print “You lose. The word was: ” followed by the secret word.
Microsoft Visual C# An Introduction to Object-Oriented Programming
ISBN: 978-1337102100
7th edition
Authors: Joyce Farrell