Skip to content

Instantly share code, notes, and snippets.

@NoelMacwan
Created April 21, 2017 15:32
Show Gist options
  • Save NoelMacwan/5d69eb12500275a323e1465f3a2afa61 to your computer and use it in GitHub Desktop.
Save NoelMacwan/5d69eb12500275a323e1465f3a2afa61 to your computer and use it in GitHub Desktop.
# This program fetches capitals file from working directory,
# converts 'list' which has list of countries and 'dict' which maps countries and its capitals.
# The list of countries will be shuffled and user will be asked to its capital
import random
c=0
f = open("capitals.txt", "r+")
str = f.read();
#Split colon and whitespaces
li=str.split(':') and str.split()
while ':' in li:
li.remove(':')
# Hashmap for 'country' and 'capital'
new = dict(zip(li[::2],li[1::2]))
# Create 'list' for country from 'dict'
country=list(new.keys())
random.shuffle(country)
for i in range(len(country)):
print("\nCapital of %s ?"%(country[i]))
ans=input()
if new[country[i]] == ans:
print("Your answer is correct")
c=c+1;
else: print("Your answer is incorrect")
print("\nYou answered %d times correctly"%(c))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment