Creating the “Indian State Game” using Pandas in 5 easy steps

Ayush Dixit
Nerd For Tech
Published in
5 min readOct 16, 2021
“Guess the Indian State” Game created by me. YT Channel : Code With Ayush

It’s amazing what one can do with the “turtle” and the “pandas” library of python. This project is one of its kind in its implementation as it hasn’t been implemented anywhere else online even the turtle coordinate dataset was created by me. It was inspired by the United States — Map Quiz game and I thought of implementing the same for my beloved country India in Python. Since, anyways India and Snakes always go together :) .

Without further ado. Let’s jump right into it.

Objective of the game

The player has to guess all the Indian States along with the Union Territories. If the Guess is correct the name of the state appears on the Indian map where the state is located geographically. The objective of the game is to populate the Indian map with all the States/UT’s.

One can exit at any point when the user likes too. On exit, the game outputs a .csv file which contains the list of the states not guessed by the user and helps in his learning of the whole map.

Steps to build the Indian State Game

  1. Importing the turtle and the pandas library.
  2. Create the turtle with the Indian map image.
  3. Create a dataset containing the coordinates of every Indian State and Union Territory in X and Y axis.
  4. Logic building for populating the map with user answers and writing on the map.
  5. Maintaing a .csv file which contains the Indian States not guessed by the user and framing the Exit logic.

Step 1 : Importing the turtle and the pandas library

import turtle
import pandas as pd

You need to install the pandas package if not already done so.

Step 2 : Create the turtle with the Indian map image.

screen = turtle.Screen()
screen.title("India States Game") # Sets the title of the window
image = "India-locator-map-blank.gif" # Shown below
screen.addshape(image)
turtle.shape(image)

“screen” is the object of the Screen() class of turtle.

“India-locator-map-blank.gif” created by me with some edits of course :) .

You can download the above India map which i made for our use case. Don’t forget to give me credit. I bet you won’t. ;)

screen.addshape(image)

In the earlier projects we got to see how the turtle shape can be modified with a square or a circle. Turtle module is amazing in the sense that it can also add an image as the turtle shape. Which can be done with the above line of code.

Here image is our “India-locator-map-blank.gif” it works well with gif and hence i had to first convert the image into a gif.

Step 3 : Create a dataset containing the coordinates of every Indian State and Union Territory in X and Y axis.

import turtle
import pandas as pd
screen = turtle.Screen()
screen.title("India States Game")
image = "India-locator-map-blank.gif"
screen.addshape(image)
turtle.shape(image)
def mouse_click_coords(x ,y):
print(x, y)

# onscreenclick is an event listener
turtle.onscreenclick(mouse_click_coords)
# wkwk
turtle.mainloop()

For getting the mouse cordinates with each mouse click on the map i took help of .onscreenclick method of the screen class. This was done in another file.

After creating the dataset which contains the State, X -Axis coordinate and Y-Axis Coordinates we are ready to design the logic for populating the map with user inputs.

Indian State game Dataset created

Step 4 : Logic building for populating the map with user answers and writing on the map.

guessed_states = []
states_not_guessed = []

while len(guessed_states) < 38:
all_states = india_dataset.State.to_list()
answer_state = screen.textinput(title=f"{len(guessed_states)}/38 States Correct", prompt="What's the state's name?").title()

We define two empty lists “guessed_states” and “states_not_guessed”. The while loop exits when all the 38 Indian States/UT’s are guessed by the user.

all_states = india_dataset.State.to_list()

Converts the “State” column of the india_dataset to a list.

answer_state = screen.textinput(title=f"{len(guessed_states)}/38 States Correct", prompt="What's the state's name?").title()

Gets the user input from the dialog box. I have given “.title()” to account for any misspellings by the user.

if answer_state in all_states:
guessed_states.append(answer_state)
writing_turle = turtle.Turtle()
writing_turle.hideturtle() #hides the turtle
writing_turle.penup()# hides the lines made by turtle when drawing
answered_state_Date_row = india_dataset[india_dataset.State == answer_state]
writing_turle.goto(int(answered_state_Date_row.X ), int(answered_state_Date_row.Y))
writing_turle.write(answer_state)
# writing_turle.write(answered_state_Date_row.State.item())
#Returns the first element of the underlying data as a python scalarr

If the user answered state is correct that is in the “all_states” list. Than we append the answer to the guessed_states list.

We make another turtle object called “wriring_turtle” for writing on the map.

answered_state_Date_row = india_dataset[india_dataset.State == answer_state]
writing_turle.goto(int(answered_state_Date_row.X ), int(answered_state_Date_row.Y))
writing_turle.write(answer_state)

We first extract the row of the answered state by the user and it gets stored in the answered_state_Date_row.

answered_state_Date_row = india_dataset[india_dataset.State == answer_state]

india_dataset.State == answer_state → compares each state in the india_dataset to the answer_state.

writing_turle.goto(int(answered_state_Date_row.X ), int(answered_state_Date_row.Y))
writing_turle.write(answer_state)

Now we need to make sure that the writing_turtle moves to the x and y coordinates which can be done by the goto method. The coordinates need to be type converted to “int” from “str”.

Step 5 : Maintaing a .csv file which contains the Indian States not guessed by the user and framing the Exit logic.

if answer_state == "Exit":
states_not_guessed = list(set(all_states) - set(guessed_states))
states_to_learn_dataframe = pd.DataFrame(states_not_guessed, columns=['States to learn'])
states_to_learn_dataframe.to_csv('States not guessed need to learn-2.csv')
break

While loop should exit when the user inputs “Exit” title case remember?

To make a list of the number of states not guessed by the user we simply subtract our two lists by first converting them to sets.

states_not_guessed = list(set(all_states) - set(guessed_states))

We now convert our states_not_guessed list to a dataframe. Which can be done by “.DataFrame” method of pandas.

states_to_learn_dataframe = pd.DataFrame(states_not_guessed, columns=['States to learn'])

Lastly, we export the .csv file containing the states not guessed by the user.

states_to_learn_dataframe.to_csv('States not guessed need to learn.csv')\
break

To break the loop lastly we give break. :)

I hope the code was easy and readable enough to understanding. You can find the complete code here.

Summary

We successfully developed the India States — Map Quiz game and in the process we learnt about the pandas module, dataframes, changing the shape of a turtle to an image, converting dtatframes to list, practical implementation of sets and exporting a dataframe to “.csv” file.

This game could be a great learning experience not only for coders but anyone who wants to do learn all the Indian States(esp Kids).

Did you like my efforts? If Yes, please follow me to get my latest posts and updates or better still, buy me a coffee!☕

--

--

Ayush Dixit
Nerd For Tech

Hi, I’m a postgraduate from IIT-Indore(M.Tech). Specialization in Comm. Signal Processing and Machine Learning/AI. Presently working as an Engineer in Qualcomm.