Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Skeleton Program for the AQA AS Summer 2018 examination
- # this code should be used in conjunction with the Preliminary Material
- # written by the AQA AS Programmer Team
- # developed in a Python 3 environment
- # Version Number : 1.6
- SPACE = ' '
- EOL = '#'
- EMPTYSTRING = ''
- def ReportError(s):
- print('{0:<5}'.format('*'),s,'{0:>5}'.format('*'))
- # function that is looking at the leading (first) character in the transmission
- def StripLeadingSpaces(Transmission):
- # getting the length / amount of characters in the transmission
- TransmissionLength = len(Transmission)
- # if there is more than nothing in the transmission
- # checking to see if the first character is a space
- # if it is a space, the length is reduced by 1
- if TransmissionLength > 0:
- FirstSignal = Transmission[0]
- while FirstSignal == SPACE and TransmissionLength > 0:
- TransmissionLength -= 1
- Transmission = Transmission[1:]
- if TransmissionLength > 0:
- FirstSignal = Transmission[0]
- # if the transmission is 0
- # it displays an error message that there are no signals in the file
- if TransmissionLength == 0:
- ReportError("No signal received")
- return Transmission
- # looking at the rest of the characters in the transmission
- def StripTrailingSpaces(Transmission):
- LastChar = len(Transmission) - 1
- while Transmission[LastChar] == SPACE:
- LastChar -= 1
- Transmission = Transmission[:-1]
- return Transmission
- # EDIT
- # attempt at changing the GetTransmission Function
- # now allows a direct transmission input rather than having to have a .txt file to read from
- def GetTransmission():
- # asks the user if they would like to load a file or direct input for translation
- choice = input("Would you like to load a file? (Yes/No): ")
- if choice == "Yes":
- # if user wants to load file, code remains the same and user inputs the file name
- # the transmission in the loaded file will be then translated into english and morse code
- FileName = input("Enter file name: ")
- try:
- FileHandle = open(FileName, 'r')
- Transmission = FileHandle.readline()
- FileHandle.close()
- Transmission = StripLeadingSpaces(Transmission)
- if len(Transmission) > 0:
- Transmission = StripTrailingSpaces(Transmission)
- Transmission = Transmission + EOL
- except:
- # if there is nothing in the file (len=0) then this message is dispalyed
- ReportError("No transmission found")
- Transmission = EMPTYSTRING
- else:
- # if the user decides to input a transmission directly, they will input 'No' instead
- # prompt message to get the user to input the transmission
- # note that only = and spaces are allowed
- Transmission = input("Enter your transmission String (Use '=' and spaces only ): ")
- Transmission = StripLeadingSpaces(Transmission)
- # if there is a transmission submitted correctly then it will translate it and return the translations
- if len(Transmission) > 0:
- Transmission = StripTrailingSpaces(Transmission)
- Transmission = Transmission + EOL
- return Transmission
- def GetNextSymbol(i, Transmission):
- if Transmission[i] == EOL:
- print()
- print("End of transmission")
- Symbol = EMPTYSTRING
- else:
- SymbolLength = 0
- Signal = Transmission[i]
- while Signal != SPACE and Signal != EOL:
- i += 1
- Signal = Transmission[i]
- SymbolLength += 1
- if SymbolLength == 1:
- Symbol = '.'
- elif SymbolLength == 3:
- Symbol = '-'
- elif SymbolLength == 0:
- Symbol = SPACE
- else:
- ReportError("Non-standard symbol received")
- Symbol = EMPTYSTRING
- return i, Symbol
- def GetNextLetter(i, Transmission):
- SymbolString = EMPTYSTRING
- LetterEnd = False
- while not LetterEnd:
- i, Symbol = GetNextSymbol(i, Transmission)
- if Symbol == SPACE:
- LetterEnd = True
- i += 4
- elif Transmission[i] == EOL:
- LetterEnd = True
- elif Transmission[i + 1] == SPACE and Transmission[i + 2] == SPACE:
- LetterEnd = True
- i += 3
- else:
- i += 1
- SymbolString = SymbolString + Symbol
- return i, SymbolString
- def Decode(CodedLetter, Dash, Letter, Dot):
- CodedLetterLength = len(CodedLetter)
- Pointer = 0
- for i in range(CodedLetterLength):
- Symbol = CodedLetter[i]
- if Symbol == SPACE:
- return SPACE
- elif Symbol == '-':
- Pointer = Dash[Pointer]
- else:
- Pointer = Dot[Pointer]
- return Letter[Pointer]
- def ReceiveMorseCode(Dash, Letter, Dot):
- PlainText = EMPTYSTRING
- MorseCodeString = EMPTYSTRING
- Transmission = GetTransmission()
- LastChar = len(Transmission) - 1
- i = 0
- while i < LastChar:
- i, CodedLetter = GetNextLetter(i, Transmission)
- MorseCodeString = MorseCodeString + SPACE + CodedLetter
- PlainTextLetter = Decode(CodedLetter, Dash, Letter, Dot)
- PlainText = PlainText + PlainTextLetter
- print(MorseCodeString)
- print(PlainText)
- # when the user wants to send morse code
- def SendMorseCode(MorseCode):
- # asks the user to enter a message using only uppercase and spaces
- PlainText = input("Enter your message (uppercase letters and spaces only): ")
- # calculating the amount of characters
- PlainTextLength = len(PlainText)
- MorseCodeString = EMPTYSTRING
- # for each character in the message submitted by the user
- # finds the morse code equivalent and adds them to the morse code string to make a morse code phrase or word
- for i in range(PlainTextLength):
- PlainTextLetter = PlainText[i]
- if PlainTextLetter == SPACE:
- Index = 0
- else:
- Index = ord(PlainTextLetter) - ord('A') + 1
- CodedLetter = MorseCode[Index]
- MorseCodeString = MorseCodeString + CodedLetter + SPACE
- print(MorseCodeString)
- # this creates the menu layout
- def DisplayMenu():
- print()
- print("Main Menu")
- print("=========")
- print("R - Receive Morse code")
- print("S - Send Morse code")
- print("X - Exit program")
- print()
- # GetMenuOption function is used to ask the user what they would like to do with the program
- def GetMenuOption():
- MenuOption = EMPTYSTRING
- # while the input is not 1
- # as the input only requires a single letter, the program will display the prompt when the box is empty
- while len(MenuOption) != 1:
- MenuOption = input("Enter your choice: ")
- return MenuOption
- def SendReceiveMessages():
- 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]
- 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]
- 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']
- # storing the morse code for each letter in a MorseCode list
- MorseCode = [' ','.-','-...','-.-.','-..','.','..-.','--.','....','..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...','-','..-','...-','.--','-..-','-.--','--..']
- # Deciding what each input will do
- # when the code is run, programEnd = False as it is running
- ProgramEnd = False
- # while the program is running, display the menu for the user
- while not ProgramEnd:
- DisplayMenu()
- #calls the menu function to ask the user for their input
- MenuOption = GetMenuOption()
- # if the input is R, it calls the ReceiveMorseCode function
- if MenuOption == 'R':
- ReceiveMorseCode(Dash, Letter, Dot)
- # if the input is S, it calls the SendMorseCode function
- elif MenuOption == 'S':
- SendMorseCode(MorseCode)
- # 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.
- elif MenuOption == 'X':
- ProgramEnd = True
- # 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
- else:
- print("This is an invalid input, please try again: ")
- if __name__ == "__main__":
- SendReceiveMessages()
Advertisement
Add Comment
Please, Sign In to add comment