|
| 1 | +morsecode = { |
| 2 | + 'A' : '. -', 'B' : '- . . .', 'C' : '- . - .', 'D' : '- . .', |
| 3 | + 'E' : '.', 'F' : '. . - .', 'G' : '- - .', 'H' : '. . . .', |
| 4 | + 'I' : '. .', 'J' : '. - - -', 'K' : '- . -', 'L' : '. - . .', |
| 5 | + 'M' : '- -', 'N' : '- .', 'O' : '- - -', 'P' : '. - - .', |
| 6 | + 'Q' : '- - . -', 'R' : '. - .', 'S' : '. . .', 'T' : '-', |
| 7 | + 'U' : '. . -', 'V' : '. . . -', 'W' : '. - -', 'X' : '- . . -', |
| 8 | + 'Y' : '- . - -', 'Z' : '- - . .', '1' : '. - - - -', '2' : '. . - - -', |
| 9 | + '3' : '. . . - -', '4' : '. . . . -', '5' : '. . . . .', '6' : '- . . . .', |
| 10 | + '7' : '- - . . .', '8' : '- - - . .', '9' : '- - - - .', '0' : '- - - - -', |
| 11 | + ',' : '- - . . - -', '.' : '. - . - . -', '?' : '. . - - . .', '/' : '- . . - .', |
| 12 | + '-' : '- . . . . -', '(' : '- . - - .', ')' : '- . - - . -', |
| 13 | +} |
| 14 | + |
| 15 | +def encrypt(message): |
| 16 | + new_message = '' |
| 17 | + for letter in message: |
| 18 | + if letter != ' ': |
| 19 | + new_message += morsecode[letter] + ' '*3 |
| 20 | + else: |
| 21 | + new_message += ' '*7 |
| 22 | + return new_message |
| 23 | + |
| 24 | +def decrypt(message): |
| 25 | + message += ' ' |
| 26 | + new_message = '' |
| 27 | + alpha = '' |
| 28 | + i = 0 |
| 29 | + for letter in message: |
| 30 | + if letter != ' ': |
| 31 | + if i ==1: |
| 32 | + alpha += ' ' |
| 33 | + i = 0 |
| 34 | + alpha += letter |
| 35 | + else: |
| 36 | + i += 1 |
| 37 | + if i == 3: |
| 38 | + new_message += list(morsecode.keys())[list(morsecode.values()).index(alpha)] |
| 39 | + alpha ='' |
| 40 | + if i == 7: |
| 41 | + new_message += ' ' |
| 42 | + return new_message |
| 43 | + |
| 44 | + |
| 45 | +def welcome(): |
| 46 | + print("\n\nHello Welcome to Morse code Translator") |
| 47 | + print("You have following options :") |
| 48 | + print("1: Morse-code to english text") |
| 49 | + print("2: English text to Morse-code") |
| 50 | + print("##### Please input your choice by the numbers 1 or 2 ###### ") |
| 51 | + print("##### Enter q to exit ######") |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + welcome() |
| 56 | + while True: |
| 57 | + choice = input() |
| 58 | + if choice == 'q': |
| 59 | + print("Thanks for visiting!!!") |
| 60 | + break |
| 61 | + try: |
| 62 | + choice = int(choice) |
| 63 | + if choice == 1: |
| 64 | + print("Input your Morse-code") |
| 65 | + code = input() |
| 66 | + eng_text = decrypt(code.lower()) |
| 67 | + print("Here's your english text") |
| 68 | + print(eng_text) |
| 69 | + welcome() |
| 70 | + if choice == 2: |
| 71 | + print("Input your english text") |
| 72 | + eng_text = input() |
| 73 | + code = encrypt(eng_text.upper()) |
| 74 | + print("Here's your Morse-code") |
| 75 | + print(code) |
| 76 | + welcome() |
| 77 | + |
| 78 | + except Exception as e: |
| 79 | + print("Invalid choice please choose again!") |
| 80 | + print(e) |
| 81 | + |
| 82 | + |
0 commit comments