Prefix quiz in Python

Tagged:

For anyone who wants drill hamradio prefixes like me, I wrote this simple python script. It is my next python exercise. It works only one way: Question is prefix, Options are DXCC entity names. It is based on cty.dat. It is available at www.country-files.com and must be in same directory as code below. Hit ctrl-c or ctrl-break to exit app.

Have a fun !

  1. import re
  2. import random
  3.  
  4. dxcc = {}
  5.  
  6. txt = open('cty.dat').read()
  7. raw = re.findall('(.*?):\s+(\d+):\s+(\d+):\s+(\w{2}):\s+([-\d.]+):\s+([-\d.]+):\s+([-\d.]+):\s+([a-z0-9\/*]+):\n\s+(.*?;)\n', txt , re.S|re.I)
  8. for e in raw:
  9.     dxcc[e[7].lower()] = e[0]
  10.  
  11. correct = 0
  12. wrong = 0
  13.  
  14. while 1:
  15.     answer = 0
  16.     pfx = dxcc.keys()
  17.     random.shuffle( pfx )
  18.     question = random.randint(0,3)
  19.  
  20.     while answer not in ("1","2","3","4"):
  21.         print "=== "+ pfx[question].upper() + " ===\n"
  22.         for i in range(0,4):
  23.             print str(i+1)+') '+ dxcc[ pfx[i] ]
  24.         answer = raw_input("\n"+str(correct)+":"+str(wrong)+"?")
  25.  
  26.     if question == int(answer)-1:
  27.         print "Correct!\n\n"
  28.         correct += 1
  29.     else:
  30.         print "Wrong! -----> Correct is " + dxcc[ pfx[question] ] + "\n\n"
  31.         wrong += 1