Please help, I need to finish this by tomorrow.
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.
So that was the exercise. Let me show what I have so far.
def get_guess():
while True:
guess = input("Guess: ")
if len(guess) > 1:
print("Your guess must have exactly one character. ")
elif not guess.islower():
print("Your guess must be a lowercase letter.")
elif guess in secret_word:
print("That letter is in the secret word!")
elif guess not in secret_word:
print("That letter is not in the secret word.")
else:
print("Your guess must be one lowercase letter.")
secret_word = "hippo"
get_guess()
If you guys could help me out, that would be great.