LouieFord

Untitled

Jun 13th, 2022
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.89 KB | None | 0 0
  1. # Skeleton Program for the AQA AS Summer 2018 examination
  2. # this code should be used in conjunction with the Preliminary Material
  3. # written by the AQA AS Programmer Team
  4. # developed in a Python 3 environment
  5.  
  6.  
  7. # Version Number : 1.6
  8.  
  9. SPACE = ' '
  10. EOL = '#'
  11. EMPTYSTRING = ''
  12.  
  13. def ReportError(s):
  14. print('{0:<5}'.format('*'),s,'{0:>5}'.format('*'))
  15.  
  16. # function that is looking at the leading (first) character in the transmission
  17. def StripLeadingSpaces(Transmission):
  18. # getting the length / amount of characters in the transmission
  19. TransmissionLength = len(Transmission)
  20. # if there is more than nothing in the transmission
  21. # checking to see if the first character is a space
  22. # if it is a space, the length is reduced by 1
  23. if TransmissionLength > 0:
  24. FirstSignal = Transmission[0]
  25. while FirstSignal == SPACE and TransmissionLength > 0:
  26. TransmissionLength -= 1
  27. Transmission = Transmission[1:]
  28. if TransmissionLength > 0:
  29. FirstSignal = Transmission[0]
  30. # if the transmission is 0
  31. # it displays an error message that there are no signals in the file
  32. if TransmissionLength == 0:
  33. ReportError("No signal received")
  34. return Transmission
  35.  
  36. # looking at the rest of the characters in the transmission
  37. def StripTrailingSpaces(Transmission):
  38. LastChar = len(Transmission) - 1
  39. while Transmission[LastChar] == SPACE:
  40. LastChar -= 1
  41. Transmission = Transmission[:-1]
  42. return Transmission
  43.  
  44. # EDIT
  45. # attempt at changing the GetTransmission Function
  46. # now allows a direct transmission input rather than having to have a .txt file to read from
  47. def GetTransmission():
  48. # asks the user if they would like to load a file or direct input for translation
  49. choice = input("Would you like to load a file? (Yes/No): ")
  50. if choice == "Yes":
  51. # if user wants to load file, code remains the same and user inputs the file name
  52. # the transmission in the loaded file will be then translated into english and morse code
  53. FileName = input("Enter file name: ")
  54. try:
  55. FileHandle = open(FileName, 'r')
  56. Transmission = FileHandle.readline()
  57. FileHandle.close()
  58. Transmission = StripLeadingSpaces(Transmission)
  59. if len(Transmission) > 0:
  60. Transmission = StripTrailingSpaces(Transmission)
  61. Transmission = Transmission + EOL
  62. except:
  63. # if there is nothing in the file (len=0) then this message is dispalyed
  64. ReportError("No transmission found")
  65. Transmission = EMPTYSTRING
  66. else:
  67. # if the user decides to input a transmission directly, they will input 'No' instead
  68. # prompt message to get the user to input the transmission
  69. # note that only = and spaces are allowed
  70. Transmission = input("Enter your transmission String (Use '=' and spaces only ): ")
  71. Transmission = StripLeadingSpaces(Transmission)
  72. # if there is a transmission submitted correctly then it will translate it and return the translations
  73. if len(Transmission) > 0:
  74. Transmission = StripTrailingSpaces(Transmission)
  75. Transmission = Transmission + EOL
  76. return Transmission
  77.  
  78.  
  79. def GetNextSymbol(i, Transmission):
  80. if Transmission[i] == EOL:
  81. print()
  82. print("End of transmission")
  83. Symbol = EMPTYSTRING
  84. else:
  85. SymbolLength = 0
  86. Signal = Transmission[i]
  87. while Signal != SPACE and Signal != EOL:
  88. i += 1
  89. Signal = Transmission[i]
  90. SymbolLength += 1
  91. if SymbolLength == 1:
  92. Symbol = '.'
  93. elif SymbolLength == 3:
  94. Symbol = '-'
  95. elif SymbolLength == 0:
  96. Symbol = SPACE
  97. else:
  98. ReportError("Non-standard symbol received")
  99. Symbol = EMPTYSTRING
  100. return i, Symbol
  101.  
  102. def GetNextLetter(i, Transmission):
  103. SymbolString = EMPTYSTRING
  104. LetterEnd = False
  105. while not LetterEnd:
  106. i, Symbol = GetNextSymbol(i, Transmission)
  107. if Symbol == SPACE:
  108. LetterEnd = True
  109. i += 4
  110. elif Transmission[i] == EOL:
  111. LetterEnd = True
  112. elif Transmission[i + 1] == SPACE and Transmission[i + 2] == SPACE:
  113. LetterEnd = True
  114. i += 3
  115. else:
  116. i += 1
  117. SymbolString = SymbolString + Symbol
  118. return i, SymbolString
  119.  
  120. def Decode(CodedLetter, Dash, Letter, Dot):
  121. CodedLetterLength = len(CodedLetter)
  122. Pointer = 0
  123. for i in range(CodedLetterLength):
  124. Symbol = CodedLetter[i]
  125. if Symbol == SPACE:
  126. return SPACE
  127. elif Symbol == '-':
  128. Pointer = Dash[Pointer]
  129. else:
  130. Pointer = Dot[Pointer]
  131. return Letter[Pointer]
  132.  
  133. def ReceiveMorseCode(Dash, Letter, Dot):
  134. PlainText = EMPTYSTRING
  135. MorseCodeString = EMPTYSTRING
  136. Transmission = GetTransmission()
  137. LastChar = len(Transmission) - 1
  138. i = 0
  139. while i < LastChar:
  140. i, CodedLetter = GetNextLetter(i, Transmission)
  141. MorseCodeString = MorseCodeString + SPACE + CodedLetter
  142. PlainTextLetter = Decode(CodedLetter, Dash, Letter, Dot)
  143. PlainText = PlainText + PlainTextLetter
  144. print(MorseCodeString)
  145. print(PlainText)
  146.  
  147. # when the user wants to send morse code
  148. def SendMorseCode(MorseCode):
  149. # asks the user to enter a message using only uppercase and spaces
  150. PlainText = input("Enter your message (uppercase letters and spaces only): ")
  151. # calculating the amount of characters
  152. PlainTextLength = len(PlainText)
  153. MorseCodeString = EMPTYSTRING
  154. # for each character in the message submitted by the user
  155. # finds the morse code equivalent and adds them to the morse code string to make a morse code phrase or word
  156. for i in range(PlainTextLength):
  157. PlainTextLetter = PlainText[i]
  158. if PlainTextLetter == SPACE:
  159. Index = 0
  160. else:
  161. Index = ord(PlainTextLetter) - ord('A') + 1
  162. CodedLetter = MorseCode[Index]
  163. MorseCodeString = MorseCodeString + CodedLetter + SPACE
  164. print(MorseCodeString)
  165.  
  166. # this creates the menu layout
  167. def DisplayMenu():
  168. print()
  169. print("Main Menu")
  170. print("=========")
  171. print("R - Receive Morse code")
  172. print("S - Send Morse code")
  173. print("X - Exit program")
  174. print()
  175.  
  176. # GetMenuOption function is used to ask the user what they would like to do with the program
  177. def GetMenuOption():
  178. MenuOption = EMPTYSTRING
  179. # while the input is not 1
  180. # as the input only requires a single letter, the program will display the prompt when the box is empty
  181. while len(MenuOption) != 1:
  182. MenuOption = input("Enter your choice: ")
  183. return MenuOption
  184.  
  185. def SendReceiveMessages():
  186. Dash = [20,23,0,0,24,1,0,17,0,21,0,25,0,15,11,0,0,0,0,22,13,0,0,10,0,0,0]
  187. Dot = [5,18,0,0,2,9,0,26,0,19,0,3,0,7,4,0,0,0,12,8,14,6,0,16,0,0,0]
  188. Letter = [' ','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
  189.  
  190. # storing the morse code for each letter in a MorseCode list
  191. MorseCode = [' ','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..']
  192.  
  193. # Deciding what each input will do
  194. # when the code is run, programEnd = False as it is running
  195. ProgramEnd = False
  196. # while the program is running, display the menu for the user
  197. while not ProgramEnd:
  198. DisplayMenu()
  199. #calls the menu function to ask the user for their input
  200. MenuOption = GetMenuOption()
  201. # if the input is R, it calls the ReceiveMorseCode function
  202. if MenuOption == 'R':
  203. ReceiveMorseCode(Dash, Letter, Dot)
  204. # if the input is S, it calls the SendMorseCode function
  205. elif MenuOption == 'S':
  206. SendMorseCode(MorseCode)
  207. # If the input is X, then in will turn ProgramEnd to True, meaning that the menu will no longer appear and the program will stop running.
  208. elif MenuOption == 'X':
  209. ProgramEnd = True
  210. # Incase the input does not match up to the available options, then it will ask the user for another input and display the options again
  211. else:
  212. print("This is an invalid input, please try again: ")
  213.  
  214.  
  215. if __name__ == "__main__":
  216. SendReceiveMessages()
Advertisement
Add Comment
Please, Sign In to add comment