In this exercise you will use the concepts learned in the Lessons 05 through 07.
This semester we will take inspiration from the free, viral word puzzle game Wordle. If you haven’t played it, you should! We do not yet have all the conceptual tools we will need to recreate this game (yet!) so this first exercise is exploring some of the fundamentals that will help ultimately make it possible.
Have you tried solving today’s Wordle? If not, do so first!
In this exercise, you will prompt the user for a five-character word and a single letter. You will then test to see which indices of the word match the single letter.
You should follow the steps below for implementing the program one step at a time. To get a sense of where you are going, though, consider what we expect the output to be given some example inputs:
$ python -m exercises.ex01_chardle
Enter a 5-character word: hello
Enter a single character: h
Searching for h in hello
h found at index 0
1 instance of h found in hello
$ python -m exercises.ex01_chardle
Enter a 5-character word: hello
Enter a single character: l
Searching for l in hello
l found at index 2
l found at index 3
2 instances of l found in hello
$ python -m exercises.ex01_chardle
Enter a 5-character word: weeee
Enter a single character: e
Searching for e in weeee
e found at index 1
e found at index 2
e found at index 3
e found at index 4
4 instances of e found in weeee
$ python -m exercises.ex01_chardle
Enter a 5-character word: hello
Enter a single character: z
Searching for z in hello
No instances of z found in hello
$ python -m exercises.ex01_chardle
Enter a 5-character word: tarheels
Error: Word must contain 5 characters
$ python -m exercises.ex01_chardle
Enter a 5-character word: hello
Enter a single character: hello
Error: Character must be a single character.
Part 0. Setting up the Python Program
In Visual Studio Code, be sure your workspace is open. (Reminder: File > Open Recent > comp110-YYS-workspace is a quick way to reopen it! Where YY is the current year and S is the semeseter: S for Spring, F for Fall.)
Open the Explorer pane (click the icon with two sheets of paper or to to View > Explorer) and expand the Workspace directory.
Right click on the exercises
directory and select “New File”. Enter the following filename, being careful to match capitalization and punctuation:
ex01_chardle.py
Before beginning work on the program, you should add a docstring to the top of your Python module just as you did in EX00. Then, you should add a line with the special variable named __author__
assigned to be a string with your 9-digit student PID. (Disclaimer: Out in the real world the __author__
variable is typically your name and e-mail address, but since we will grade your programs we’d like to avoid potential bias in seeing your names as part of the programs as we’re grading.) Add the following lines above the line of code that calls the print
function. Fill in your 9-digit UNC PID number, without any spaces or dashes, in the __author__
string.
"""EX01 - Chardle - A cute step toward Wordle."""
= "1234567890"
__author__
Part 1. Prompting for Inputs – 20 Points
The first task of this program is to gather inputs from the user. Using the concepts you learned in CQ02 - User Input and Variables, you will want to ask the user to enter a 5-character word and a single character, storing both in str
variables. You will then print out a diagnostic message, built-up using string concatenation, that confirms you have correctly stored each input in a variable. Please choose meaningful, descriptive names for your variables. Write your prompts and diagnostic message such that you can reproduce the following in the shell after saving and running your program:
$ python -m exercises.ex01_chardle
Enter a 5-character word: hello
Enter a single character: e
Searching for e in hello
WARNING: Autograding will very specifically be looking for exactly the format of lines output shown above. You will not see the $
at your command-line prompt in VSCode, you can ignore that part. Otherwise, when you run the program on your machine with the same inputs as above on the first two lines, your printed results should look exactly like the 3rd line.
Part 2. Checking Indices for Matches – 20 Points
Now that you have gathered your inputs from Part 1, and stored them in variables, your task is to check each index of the word string to see if it matches (hint: is equal to) the character input. These checks will involve combining concepts of:
- LS05 - Objects and Data Types
- Strings - A
str
is a Sequence of Characters
- Strings - A
- LS06 - Expressions
- Relational Operators - Equality
- LS07 - Conditional if-else Statements
Soon you will learn a more efficient way of performing these checks using a concept called a loop. For now, you will need to check each of the five indices of the word string to see if it is equal to the character string. If so, then you should print out a message indicating the letter being searched for was found at a given index. Your goal in this part is to be able to do the following:
$ python -m exercises.ex01_chardle
Enter a 5-character word: hello
Enter a single character: e
Searching for e in hello
e found at index 1
$ python -m exercises.ex01_chardle
Enter a 5-character word: heels
Enter a single character: e
Searching for e in heels
e found at index 1
e found at index 2
$ python -m exercises.ex01_chardle
Enter a 5-character word: heels
Enter a single character: s
Searching for s in heels
s found at index 4
Part 3. Counting Matching Indices – 30 Points
Be sure you have completed Part 2 before beginning Part 3!
Now that you are finding the indices of the word that match the character being searched for, establish a variable to count the number of matching characters. Before you find any matching characters, what should you initialize this variable to? Each time you find a match, increase your counting variable’s value by 1. Finally, print out a message that indicates how many instances of the character you found in the input word.
You will need to exactly match the output formatting of the examples below. Specifically, notice, there are different messages for when no matches are found, a singular match is found, or multiple matches are found (i.e.: No instances vs. 1 instance vs. 2 instances). You will need to use a nested if-else
statement in order to output these messages correctly. Correction: you do not need to use a nested ‘if-else’ statement.
$ python3 -m exercises.ex01_chardle
Enter a 5-character word: hello
Enter a single character: e
Searching for e in hello
e found at index 1
1 instance of e found in hello
$ python3 -m exercises.ex01_chardle
Enter a 5-character word: heels
Enter a single character: e
Searching for e in heels
e found at index 1
e found at index 2
2 instances of e found in heels
$ python3 -m exercises.ex01_chardle
Enter a 5-character word: heels
Enter a single character: d
Searching for d in heels
No instances of d found in heels
Part 4. Exiting Early for Invalid Inputs - 10 points
What happens if you input a word with fewer than 5 characters? Or you input a “char” that is more than one character, or none at all? It’s good practice to handle bad input from an end-user gracefully in your programs. Our strategy, for now, will be to simply print an error message and then exit the program early. There is a special built-in function called exit()
that will send a signal to your system and tell the program to quit at that point, not continuing on further in the program. Before attempting to implement the following behavior in your program, think about where it logically makes sense to try adding these checks for correct input. You will need to use the len
function, as described in Lesson 5 - Objects and Data Types, in order to find the length of your input strings.
Here is how your program should work after completing this part:
$ python -m exercises.ex01_chardle
Enter a 5-character word: help
Error: Word must contain 5 characters
$ python -m exercises.ex01_chardle
Enter a 5-character word: tarheels
Error: Word must contain 5 characters
$ python -m exercises.ex01_chardle
Enter a 5-character word: heels
Enter a single character: tar
Error: Character must be a single character.
$ python -m exercises.ex01_chardle
Enter a 5-character word: heels
Enter a single character:
Error: Character must be a single character.
$ python -m exercises.ex01_chardle
Enter a 5-character word: heels
Enter a single character: h
Searching for h in heels
h found at index 0
1 instance of h found in heels
Part 5. Style and Documentation Requirements – 10 Points (Manually Graded)
We will manually grade your code and are looking for good choices of meaningful variable names. Your variable names should be descriptive of their purposes. You should also use the Python snake_case
convention where variable names are all lowercase and new words are separated by underscores. You should not make excessive use of variables where single variables will suffice (e.g. you only need one counting variable for counting the instances in Part 3).
Please do not use the .count() method to get the total number of instances, this will result in a grade reduction.
Part 6. Type Safety and Linting - 9 Points
The autograder uses industry standard tools for checking the type safety of your programs (e.g. being sure you’re using variables of the correct data types in valid ways) and linting style rules. If you have point deductions on Type Safety or Linting, read through the feedback the autograder gives and it should direct you to the line number in your program with the issue.
Make a Backup Checkpoint “Commit”
As you make progress on this exercise, making backups is encouraged.
- Open the Source Control panel (Command Palette: “Show SCM” or click the icon with three circles and lines on the activity panel).
- Notice the files listed under Changes. These are files you’ve made modifications to since your last backup.
- Move your mouse’s cursor over the word Changes and notice the + symbol that appears. Click that plus symbol to add all changes to the next backup. You will now see the files listed under “Staged Changes”.
- If you do not want to backup all changed files, you can select them individually. For this course you’re encouraged to back everything up.
- In the Message box, give a brief description of what you’ve changed and are backing up. This will help you find a specific backup (called a “commit”) if needed. In this case a message such as, “Progress on Exercise 1” will suffice.
- Press the Check icon to make a Commit (a version) of your work.
- Finally, press the Ellipses icon (…), look for “Pull/Push” submenu, and select “Push to…”, and in the dropdown select your backup repository.
Submit to Gradescope for Grading
All that’s left now is to hand-in your work on Gradescope for grading!
Login to Gradescope and select the assignment named “EX01 - Chardle”. You’ll see an area to upload a zip file. To produce a zip file for autograding, return back to Visual Studio Code.
If you do not see a Terminal at the bottom of your screen, open the Command Palette and search for “View: Toggle Integrated Terminal”.
Type the following command (all on a single line):
python -m tools.submission exercises/ex01_chardle.py
In the file explorer pane, look to find the zip file named “yy.mm.dd-hh.mm-exercises-ex01_chardle.py.zip”. The “yy”, “mm”, “dd”, and so on, are timestamps with the current year, month, day, hour, minute. If you right click on this file and select “Reveal in File Explorer” on Windows or “Reveal in Finder” on Mac, the zip file’s location on your computer will open. Upload this file to Gradescope to submit your work for this exercise.
Autograding will take a few moments to complete. For this exercise there will be 10 “human graded” points. Thus, you should expect a maximum autograding score of 90 possible points on this assignment. If there are issues reported, you are encouraged to try and resolve them and resubmit. If for any reason you aren’t receiving full credit and aren’t sure what to try next, come give us a visit in office hours!