z_calcul_mental_old.py

Created by cent20

Created on April 19, 2021

2.87 KB

Une précédente version 100% texte


v = "1.0" # Calcul mental, Par Robert V. et Fedyna K.

from random import randint as rd

# Difficulte
dif = {"+":[[[1,9],[1,9]],[[10,19],[1,9]],[[10,99],[1,9]],[[10,99],[10,99]]],
       "-":[[[1,9],[1,9]],[[10,99],[1,9]],[[10,20],[10,20]],[[10,99],[10,99]]],
       "*":[[[1,10],[1,10]],[[1,10],[1,10]],[[2,10],[11,20]],[[1,25],[1,25]]],
       "/":[[[1,10],[2,10]],[[1,10],[2,10]],[[1,10],[2,10]],[[1,20],[2,20]]]}

# elements du menu
cm = ["# Automatismes et calcul mental", "Addition", "Soustraction", "Multiplication", "division"]
lcm = len(cm)

# liste des parametres
par = [["Dif.", "facile", "moyen", "difficile", "expert"],
       ["Rép.", 16, 32, 64, 128],
       ["Mode", "classique", "ecole", "college", "lycee"]]
lpar = len(par)
p = [1, 1, 1] # parametres par défaut

def mel(a,b):
    l = [a,b]
    a = l[rd(0,1)]
    l.remove(a)
    b = l[0]
    return a,b

def calcul(op,d):
    # Le generateur de calcul
    
    # Variables : -1 pour etre lisible par code
    op = ["+","-","*","/"][op-1]
    d -= 1
    
    # Difficulté

    exeMu = list(range(1,6))
    exeMu.append(10)
    exeDiv = [[2,10],[2,3,5,10]]
    
    while "exceptions gérées":
        a = rd(dif[op][d][0][0],dif[op][d][0][1])
        b = rd(dif[op][d][1][0],dif[op][d][1][1])
        if op in "+-*":
            a,b = mel(a,b)
        if op == "*" and d == 0:
            if a in exeMu and b in exeMu:
                break
        elif op == "-" and d == 0:
            if a >= b:
                break
        elif op == "/":
            a = a*b
            if d in [0,1,2] and a <= 100:
                if d in [0,1] and b in exeDiv[d]:
                    break
                if d == 2:
                    break
            if d == 3 and a <= 200:
                break
        else:
            break
    
    # Demande de l'operation (uniquement pour test console)
    c = int(input(str(a)+" "+op+" "+str(b)+" = "))
    
    # Verification du resultat (a voir avec les divisions s'il faut le changer)
    if c == int(eval(str(a)+op+str(b))):
        print("BRAVO ! ",end="")
    print(int(eval(str(a)+op+str(b))))        

def menu():
    print(cm[0])
    for i in range(1,lcm):
        print(str(i)*(i>0),cm[i])
    for j in range(lpar):
        print(i+j+1, par[j][0], " : ", par[j][p[j]] )
    print("9 >>> Quitter le programme")    
    c = 0
    while c==0:
        c=int(input())
    if 1 <= c <= lcm-1:
        for i in range(par[1][p[2]]):
            calcul(c,p[0]) # Tout étant stocké par p je n'ai besoin de passer que le numéro
    elif lcm <= c <= lcm+lpar+1:
        regler(c-lcm)
    elif c == 9:
        print("Au revoir !")
        exit
    else: 
        menu() 
    if c!=9:
        menu()
        
def regler(r):
    print("R = ", r) # debug à neutraliser
    print(cm[0])
    for j in range(1,5):
        print(j, par[r][0], " : ", par[r][j] ) 
    p[r] = int(input())
    menu()
    
menu()

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.