SHARE
TWEET

Untitled

a guest Sep 19th, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import winsound
  2. import time
  3. import random
  4.  
  5. morse = {
  6.     "a": ".-",
  7.     "b": "-...",
  8.     "c": "-.-.",
  9.     "d": "-..",
  10.     "e": ".",
  11.     "f": "..-.",
  12.     "g": "--.",
  13.     "h": "....",
  14.     "i": "..",
  15.     "j": ".---",
  16.     "k": "-.-",
  17.     "l": ".-..",
  18.     "m": "--",
  19.     "n": "-.",
  20.     "o": "---",
  21.     "p": ".--.",
  22.     "q": "--.-",
  23.     "r": ".-.",
  24.     "s": "...",
  25.     "t": "-",
  26.     "u": "..-",
  27.     "v": "...-",
  28.     "w": ".--",
  29.     "x": "-..-",
  30.     "y": "-.--",
  31.     "z": "--.."
  32. }
  33.  
  34. letters = dict([[x, y] for y, x in morse.items()])
  35.  
  36. translated = []
  37.  
  38. freq = 800
  39. unit = 200
  40.  
  41. def dit():
  42.     winsound.Beep(freq, unit)
  43.  
  44.  
  45. def dah():
  46.     winsound.Beep(freq, unit * 3)
  47.  
  48.  
  49. def pause():
  50.     time.sleep(unit * 7)
  51.  
  52.  
  53. def a():
  54.     dit()
  55.     dah()
  56.  
  57.  
  58. def b():
  59.     dah()
  60.     dit()
  61.     dit()
  62.     dit()
  63.  
  64.  
  65. def c():
  66.     dah()
  67.     dit()
  68.     dah()
  69.     dit()
  70.  
  71.  
  72. def d():
  73.     dah()
  74.     dit()
  75.     dit()
  76.  
  77.  
  78. def e():
  79.     dit()
  80.  
  81.  
  82. def f():
  83.     dit()
  84.     dit()
  85.     dah()
  86.     dit()
  87.  
  88.  
  89. def g():
  90.     dah()
  91.     dah()
  92.     dit()
  93.  
  94.  
  95. def h():
  96.     dit()
  97.     dit()
  98.     dit()
  99.     dit()
  100.  
  101.  
  102. def i():
  103.     dit()
  104.     dit()
  105.  
  106.  
  107. def j():
  108.     dit()
  109.     dah()
  110.     dah()
  111.     dah()
  112.  
  113.  
  114. def k():
  115.     dah()
  116.     dit()
  117.     dah()
  118.  
  119.  
  120. def l():
  121.     dit()
  122.     dah()
  123.     dit()
  124.     dit()
  125.  
  126.  
  127. def m():
  128.     dah()
  129.     dah()
  130.  
  131.  
  132. def n():
  133.     dah()
  134.     dit()
  135.  
  136.  
  137. def o():
  138.     dah()
  139.     dah()
  140.     dah()
  141.  
  142.  
  143. def p():
  144.     dit()
  145.     dah()
  146.     dah()
  147.     dit()
  148.  
  149.  
  150. def q():
  151.     dah()
  152.     dah()
  153.     dit()
  154.     dah()
  155.  
  156.  
  157. def r():
  158.     dit()
  159.     dah()
  160.     dit()
  161.  
  162.  
  163. def s():
  164.     dit()
  165.     dit()
  166.     dit()
  167.  
  168.  
  169. def t():
  170.     dah()
  171.  
  172.  
  173. def u():
  174.     dit()
  175.     dit()
  176.     dah()
  177.  
  178.  
  179. def v():
  180.     dit()
  181.     dit()
  182.     dit()
  183.     dah()
  184.  
  185.  
  186. def w():
  187.     dit()
  188.     dah()
  189.     dah()
  190.  
  191.  
  192. def x():
  193.     dah()
  194.     dit()
  195.     dit()
  196.     dah()
  197.  
  198.  
  199. def y():
  200.     dah()
  201.     dit()
  202.     dah()
  203.     dah()
  204.  
  205.  
  206. def z():
  207.     dah()
  208.     dah()
  209.     dit()
  210.     dit()
  211.  
  212.  
  213. def translate():
  214.     global translated
  215.     dit()
  216.     raw = input("Welcome to the morse code translator! Enter a phrase below to begin! : ")
  217.  
  218.     if "." in raw or "-" in raw:
  219.         tbt = raw.split()
  220.         n = 0
  221.         for dots in tbt:
  222.             if (dots == " " and tbt[n-1]) == " ":
  223.                 translated.append(" ")
  224.             else:
  225.                 translated.append(letters[dots])
  226.             n += 1
  227.  
  228.         print("Translated to Alphabets:")
  229.         print(*translated, sep="")
  230.  
  231.     else:
  232.         for letter in raw:
  233.             if letter == " ":
  234.                 translated.append(" ")
  235.             else:
  236.                 translated.append(morse[letter])
  237.  
  238.         print("Translated to Morse:")
  239.         print(*translated)
  240.  
  241.         for i in raw:
  242.             try:
  243.                 exec(i + "()")
  244.                 time.sleep(unit * 3 / 1000)
  245.             except:
  246.                 pause()
  247.  
  248.  
  249. def trainmorse(wins, mode=0):
  250.     score = 0
  251.     wrong = []
  252.     while score < wins:
  253.         coded = random.choice("abcdefghijklmnopqrstuvwxyz")
  254.         if mode == 0:
  255.             print(morse[coded])
  256.         exec(coded + "()")
  257.         print("Type \"fff\" to hear the audio again")
  258.         guess = input("What letter is this? : ").lower()
  259.         while guess == "fff":
  260.             exec(coded + "()")
  261.             guess = input("What letter is this? : ").lower()
  262.         if guess == coded:
  263.             score += 1
  264.         else:
  265.             print("The right answer is: " + coded)
  266.             if coded not in wrong:
  267.                 wrong.append(coded)
  268.         if score != wins:
  269.             print(str(wins - score) + " more correct guesses to end training")
  270.         print()
  271.     print()
  272.     print("=== END OF TRAINING ===")
  273.     print()
  274.     print("SUMMARY OF INCORRECT LETTERS: ")
  275.     for i in wrong:
  276.         print(i.upper() + " translates to " + morse[i])
  277.     print("===== END =====")
  278.  
  279.  
  280. trainmorse(10)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top