morse.py

Created by rubimath40

Created on December 11, 2024

2.01 KB

Apprendre le morse: 1. Lire: La calculatrice affiche un mot en morse et demande de le réécrire en langage commun. 2. Ecrire: La calculatrice affiche une lettre en langage commun en morse et demande de la réécrire en morse. 3. Mot vers morse: Entrez un mot en langage commun et la calculatrice le réécrit en morse 4. Mot vers francais: Entrez un mot en morse et la calculatrice le réécrit en language commun (c’est l’objectif mais, pour l’instant, ça réécris juste en morse) 5. Quitter: arrête le programme; tapez “go()” pour le relancer


from random import*
def shufle(l):
  return sorted(l,key=lambda x:random())

c=choice
CARN=["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"]
CARM=["._","_...","._._","_..",".",".._.","__.","....","..",".___","_._","._..","__","_.","___",".__.","__._","._.","...","_",".._","..._",".__","_.._","_.__","__.."]
DCARM=[".___","_.__","__._",".__.","_.._","_._.","__..","..._",".._.","._..","_...","....","___",".__","_._","__.",".._","._.","_..","...","__","._","_.","..","_","."]
DCARN=["J","Y","Q","P","X","C","Z","V","F","L","B","H","O","W","K","G","U","R","D","S","M","A","N","I","T","E"]
MOTS=[c(CARN)+c(CARN)+c(CARN)+c(CARN)+c(CARN)for i in range(10)]

def encode(obj):
  return CARM[obj]
def decode(obj):
  return CARN[CARM.index(obj)]

def duplique(list):
  prov=[]
  for i in list:
    prov.append(i)
  return prov

def aff(txt):
  for i in txt:
    print(CARM[CARN.index(i)],end=" ")
  print()

def lire(p):
  aff(p)
  rep=input("mot ? ").upper()
  if rep==p:
    print("Bravo !")
    return 1
  else:
    print("Non...",p)
    return 0
def ecrire(p="A"):
  print("E\u0301crire",p)
  rep=input(p+": ")
  if rep==encode(CARN.index(p)):
    print("Bravo !",end="\n\n")
    return 1
  else:
    print("Non...",encode(CARN.index(p)),end="\n\n")
    return 0

def menu():
  print("1. Lire\n2. Ecrire\n3. Mot vers morse\n4. Mot vers francais\n5. Quitter")
  return input("Choix ? ")

def go(nb=10):
  while True:
    choix=menu()
    if choix not in ["1","2","3","4","5"]:
      print("Choix impossible.\n")
    elif choix=="5":
      break
    elif choix=="4":
      mot=input("mot ? ")
      for i in range(26):
        mot.replace(DCARM[i],DCARN[i])
      print(mot)
    elif choix=="3":
      mot=input("mot ? ")
      aff(mot.upper())
    else:
      if choix=="1":
        p=duplique(MOTS)
        jeu=lire
      if choix=="2":
        p=shufle(duplique(CARN))
        jeu=ecrire
      sc=0
      for i in range(nb):
        sc+=jeu(p[i])
      print("Score =",int(100/nb*sc),"%")

go()

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.