Guess a Word
00:00
Guess a Word. In this section of the course, you’ll build a very basic word-guessing game. The game won’t look good, and the feedback from it will be hard to parse if it’s present at all. Still, the building blocks for your Wordle clone will be in place by the end of this phase of the project. You’ll use input()
to read words from your players, a for
loop to give your users several guesses, and sets to find out which letters your users have guessed correctly.
00:29
You can get information from the user with input()
. This built-in function is a great way to provide simple interactivity on the command line. Open the REPL and try it out.
00:40 Enter the code seen on-screen.
00:46
You can provide an optional prompt to input()
. The user will see this before they enter any information. And as you can see, the line shows both the prompt and the user input at the same time.
00:57
The prompt asks the user to guess the word, and the user enters snake
and then hits Enter. The call to input()
returns the text that the user enters. You can see this in this example, as the string "snake"
has been assigned to guess
.
01:14
It’s never too early to start building your game. Open your editor, create the file wyrdl.py
, and enter the code as seen on-screen.
01:28
After you’ve read the user’s guess, you can check whether the guess is equal to the secret word, "SNAKE"
. You evaluate their guess and inform them whether they were correct or not.
01:39 It’s usually a good idea to create code that runs as early as possible. Even if it’s just doing something small and is far away from your end goals, making it runnable means you can start experimenting, testing, and debugging.
01:53 Now this may not look like much of a game, and even if you do think of it as one, it’s probably fairly boring. There’s little replayability because the secret word is always the same, and the feedback isn’t actionable for the user since they don’t learn much from just being told that they’re wrong.
02:10 You’ll soon improve this and make something more interesting to play. But first, you need to fix a small usability issue. Consider the game seen on-screen.
02:24
You correctly guess that the secret word is snake, but the game tells you that it’s wrong because it’s comparing your guess to the uppercase string "SNAKE"
.
02:33 Here the goal is to guess words and not to figure out whether the letters are lower or uppercase. So how can you compare two words regardless of what their case is?
02:42 The simplest solution is probably to explicitly convert the guess to uppercase. Then it doesn’t matter how the user inputs the word.
02:54
Adding .upper()
forces the user’s guess to be in uppercase. This manipulation immediately makes the game more user-friendly.
03:05 The lowercase snake guess is now reported as correct, but still, you are only giving your users one chance to guess correctly. And next, you’ll expand the game with more guesses.
Become a Member to join the conversation.