morse.py

Created by stimorol

Created on October 14, 2022

1.12 KB

Programme permettant de traduire un code du français au morse (ou l’inverse).


MORSE_CODE= {
'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':'--..', 
'1':'.----', '2':'..---', '3':'...--', 
'4':'....-', '5':'.....', '6':'-....', 
'7':'--...', '8':'---..', '9':'----.', 
'0':'-----', ', ':'--..--', '.':'.-.-.-', 
'?':'..--..', '/':'-..-.', '-':'-....-', 
'(':'-.--.', ')':'-.--.-'}

def encrypt(message): 
 cipher=''
 for letter in message:
  if letter!='/':cipher+=MORSE_CODE[letter] + '/'
  else:cipher+='/'
 print(cipher)

def decrypt(message):
 message+='/'
 decipher=citext=''
 for letter in message:
  if letter != '/':
   i=0
   citext+=letter
  else:
   i+=1
   if i==2:decipher+=' '
   else:
    decipher += list(MORSE_CODE.keys())[list(MORSE_CODE.values()).index(citext)]
    citext=''
 print(decipher)

while True:
  user=input("Quelle phrase(en morse ou\nfrancais) voulez vous traduire ?\n - ")
  if "." in user or "-" in user:decrypt(user)
  else:encrypt(user)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.