By Dr. Angela Yu
Day 25 of 100: Working with CSV Data and the Pandas Library
Using Turtle and the Pandas Data Analysis library, program a game where a user attempts to name all 50 US states.
This application is written with Python 3.11.
To play the game, a user is presented with a map of the United States and an input prompt that shows the current count of state names that have been guessed correctly.
As the user correctly guesses a state name, the name appears on the map within the shape of that particular state.
If the user cannot remember any additional state names, they can type 'exit' into the prompt to download a "States to Learn" CSV.
This project uses two libraries:
- Turtle
- Pandas
and one class:
- Label
State names and map coordinates are stored in a .csv
file and read into Python using the Pandas library.
Using the Pandas .read_csv
functionality, we read in the data for the game:
data = pandas.read_csv("50_states.csv")
We then create the Graphic User Interface (GUI) using the turtle.Screen()
function, set the game title, and load the map of the United States:
screen = turtle.Screen()
screen.title("Name All 50 US States Game")
image = "blank_states_img.gif"
screen.addshape(image)
turtle.shape(image)
Once the map is drawn, we prompt the user to Name a State and start the game:
The game continues until the user either guesses all 50 states correctly, or types exit
into the prompt.
If the user guesses all 50 states correctly, they are presented with a celebration image:
turtle.clearscreen()
image = "you_win.gif"
screen.addshape(image)
turtle.shape(image)
If the user reaches a point where they can no longer remember any state names, they then type exit
into the prompt and the application will create a "States to Learn" .csv
file, listing all of the states the user did not enter.
All of the commands below should be typed into the Python terminal of your IDE (I use PyCharm for my Python Development).
First, clone the repository from Github and switch to the new directory:
$ git clone git@github.com:shelbyblanton/us-states-game.git
Then open the project in PyCharm.
In the main.py
file, click on the word pandas
in the import statement at the top of the page. Then click on the red exclamation point and click Install Package Pandas
to load the library:
Setup is complete!
Click Run in PyCharm to see the app in action.
Programmed by M. Shelby Blanton under the instructional guidance of Dr. Angela Yu via Udemy.com.