9-Dictionary_example

We'll play a small fun quiz using Python

For this we need questions and answers to mark the user
I chose to use Indian States and their Capitals for this

We'll download a spreadsheet that has this information from this URL
By the time you are seeing this, if the link is not working, can download the spreadsheet and this notebook from my github page.

For this I've used Pandas for extracting the data from the spreadsheet and saving them as a Python dictionary. If you are just keen about the Python code for running the quiz, skip understanding these lines and just run them

Load libraries

In [4]:
import pandas as pd

Read xls file

In [7]:
path=''

Notebook and the spreadsheet are in the same directory, for you if they are in different locations, give respective path

In [8]:
xl = pd.ExcelFile(path+'list-states-india-28j.xlsx')

Read into Pandas

The sheets in this spreadsheet are

In [11]:
xl.sheet_names
Out[11]:
['Sheet1', 'Sheet2', 'Sheet3']

Our required information is in Sheet1

In [10]:
df = xl.parse('Sheet1')

Checking first 5 columns

In [56]:
df.head()
Out[56]:
States Of India Unnamed: 1 Unnamed: 2 Unnamed: 3 Unnamed: 4 Unnamed: 5 Unnamed: 6 Unnamed: 7 Unnamed: 8 Unnamed: 9 Unnamed: 10
0 S.No State Statehood ISO 3166-2:IN Vehicle Capital Largest city Population Area Official Additional official
1 NaN NaN NaN NaN code NaN NaN NaN (km) languages languages
2 1 Andhra Pradesh 1953-10-01 00:00:00 IN-AP AP Hyderabad (de jure) Visakhapatnam 49506799 160205 Telugu NaN
3 NaN NaN NaN NaN NaN Amaravati (de facto) NaN NaN NaN NaN NaN
4 2 Arunachal Pradesh 1987-02-20 00:00:00 IN-AR AR Itanagar NaN 1383727 83743 English NaN

Consider only States and Capitals columns

In [12]:
df = df[['Unnamed: 1', 'Unnamed: 5']]
In [13]:
df.head()
Out[13]:
Unnamed: 1 Unnamed: 5
0 State Capital
1 NaN NaN
2 Andhra Pradesh Hyderabad (de jure)
3 NaN Amaravati (de facto)
4 Arunachal Pradesh Itanagar

Remove first row

In [14]:
df = df.iloc[1:]
In [15]:
df.head()
Out[15]:
Unnamed: 1 Unnamed: 5
1 NaN NaN
2 Andhra Pradesh Hyderabad (de jure)
3 NaN Amaravati (de facto)
4 Arunachal Pradesh Itanagar
5 Assam Dispur

Rename column names

In [17]:
df = df.rename(columns = {'Unnamed: 1' : 'State', 'Unnamed: 5' : 'Capital'})
In [18]:
df.head()
Out[18]:
State Capital
1 NaN NaN
2 Andhra Pradesh Hyderabad (de jure)
3 NaN Amaravati (de facto)
4 Arunachal Pradesh Itanagar
5 Assam Dispur

Check null values and remove them

In [22]:
df.isnull().sum()
Out[22]:
State      6
Capital    3
dtype: int64

We can replace null values with actual State names, but we are just removing them here.

In [23]:
df = df.dropna(subset=['State'])
In [24]:
df.isnull().sum()
Out[24]:
State      0
Capital    0
dtype: int64

Fortunately removing null State values removed null Capital values also.

In [25]:
df.head()
Out[25]:
State Capital
2 Andhra Pradesh Hyderabad (de jure)
4 Arunachal Pradesh Itanagar
5 Assam Dispur
6 Bihar Patna
7 Chhattisgarh Naya Raipur

Remove extra strings in Capital names

In [27]:
df.Capital = df.Capital.apply(lambda x:x.split()[0])
In [28]:
df.Capital.head(10)
Out[28]:
2       Hyderabad
4        Itanagar
5          Dispur
6           Patna
7            Naya
8          Panaji
9     Gandhinagar
10     Chandigarh
11         Shimla
13       Srinagar
Name: Capital, dtype: object

Make lists out of Pandas dataframe

In [29]:
states = df['State'].tolist()
capitals = df['Capital'].tolist()

Make a dictionary out of these lists

In [30]:
state_capital = {}
for i in range(len(states)):
    state_capital[states[i]] = capitals[i]

Other way we can make this dictionary by using zip

state_capital = dict(zip(states,capitals))

We'll just display some top 10 values

In [37]:
{key: state_capital[key] for key in list(state_capital)[:10]}
Out[37]:
{'Andhra Pradesh': 'Hyderabad',
 'Arunachal Pradesh': 'Itanagar',
 'Assam': 'Dispur',
 'Bihar': 'Patna',
 'Chhattisgarh': 'Naya',
 'Goa': 'Panaji',
 'Gujarat': 'Gandhinagar',
 'Haryana': 'Chandigarh',
 'Himachal Pradesh': 'Shimla',
 'Jammu and Kashmir': 'Srinagar'}

Everything seems looks OK

Quiz

In [40]:
import random

Using this random module, we can generate questions randomly

In [54]:
f'What is capital of {random.choice(states)}'
Out[54]:
'What is capital of Odisha'
In [55]:
state = random.choice(states)
capital_guess = input('What is the capital of {} ?'.format(state))
if capital_guess == state_capital[state]:
    print('Correct answer')
else:
    print('Wrong answer \n Capital of {} is {}'.format(state, state_capital[state]))
What is the capital of Sikkim ?I don't know
Wrong answer
 Capital of Sikkim is Gangtok

We will keep this in a function

In [56]:
def quiz():
    num_questions = int(input('How many questions you want to take \n'))
    points = 0
    for i in range(num_questions):
        state = random.choice(states)
        capital_guess = input('What is the capital of {} ? \n'.format(state))
        if capital_guess == state_capital[state]:
            print('Correct answer')
            points +=1
        else:
            print('Wrong answer \n Capital of {} is {} \n'.format(state, state_capital[state]))
    return 'You got {} out of {}'.format(points,num_questions)

Now play the quiz

In [57]:
quiz()
How many questions you want to take
3
What is the capital of Tripura ?
Agartala
Correct answer
What is the capital of Haryana ?
Chandigarh
Correct answer
What is the capital of Bihar ?
Patna
Correct answer
Out[57]:
'You got 3 out of 3'

Below cell is for styling, ignore it

In [58]:
from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))