geophabet.py 981 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python3
  2. """
  3. # Convert words into numbers, geocaching-like
  4. # Copyright (c) 2009-2022 Michael Buesch <m@bues.ch>
  5. # Licensed under the
  6. # GNU General Public license version 2 or (at your option) any later version
  7. """
  8. import sys
  9. def convertChar(c):
  10. c = ord(c)
  11. if c >= ord('a') and c <= ord('z'):
  12. base = ord('a')
  13. elif c >= ord('A') and c <= ord('Z'):
  14. base = ord('A')
  15. else:
  16. return "UNK" # unknown
  17. return c - base + 1
  18. def convertString(string):
  19. string = string.replace(" ", "")
  20. res = ""
  21. for c in string:
  22. if res:
  23. res += ", "
  24. res += "%s=%s" % (c, str(convertChar(c)))
  25. return res
  26. def main(argv):
  27. while 1:
  28. try:
  29. string = input("ABC> ")
  30. except (EOFError, KeyboardInterrupt):
  31. break
  32. if not string:
  33. break
  34. print(convertString(string))
  35. if __name__ == "__main__":
  36. sys.exit(main(sys.argv))
  37. # vim: ts=4 sw=4 expandtab