automatismes_2de.py

Created by xanderleadaren

Created on January 21, 2026

4.38 KB

Permet aux élèves de 2de de s’entrainer sur des automatismes.

Thèmes : nombres relatifs (+, −, ×, ÷), fractions (réduire, ×, ÷, avec entiers), racines carrées, pourcentages, évolutions et variations

Plus agréable en police Python taille petite (dans les paramètres de la calculatrice)


from math import gcd, sqrt
from random import randint

# FONCTIONS MATHÉMATIQUES

def frac(num, denom):
    """Met une fraction num/denom sous forme irréductible"""
    if (num < 0 and denom < 0) or (num >= 0 and denom < 0):
        num,denom = -num, -denom
    up = num // gcd(num, denom)
    down = denom // gcd(num, denom)
    if down == 1:
        return str(up)
    else:
        return str(up) + "/" + str(down)
    
def rac(x):
    """Simplifie √x sous la forme a√b, avec b minimal"""
    if sqrt(x) == int(sqrt(x)):
        return str(int(sqrt(x)))
    else:
        div = 2
        a = 1
        while div**2 < x:
            if x % div**2 == 0: a = div
            div += 1
        b = x // a**2
        if a != 1:
            return str(a)+"sqrt("+str(b)+")"
        else:
            return "sqrt("+str(b)+")"

# FONCTIONS D’INTERACTION

def nb(valeur):
    """Affiche les éventuelles parenthèses autour d’un nombre négatif"""
    if valeur < 0:
        return "("+str(valeur)+")"
    else:
        return str(valeur)

def question(enonce, resultat, reponse):
    """Affiche la question, récupère la réponse, compte, donne l’indication"""
    global ok
    if input(enonce) == resultat:
        ok += 1
    else:
        print("ERREUR ! " + reponse + resultat)

# INITIALISATION

combien = 0
ok = 0
MAX = 12  # Nombre total de questions

# PROGRAME PRINCIPAL

while combien < MAX:
    
    # Nouvelle question
    combien += 1
    quoi = randint(1,7)

    if quoi == 1:  # Addition et soustraction d’entiers relatifs
        a = randint(-50,50)
        b = randint(-50,50)
        if randint(0,1) == 0:  # Addition
            if a > 0 and b > 0: b = -b
            text = str(a)+" + "+nb(b)+" = "
            question(text, str(a+b), text)
        else:  # Soustraction
            if a > b: a,b = b,a
            text = str(a)+" - "+nb(b)+" = "
            question(text, str(a-b), text)

    if quoi == 2:  # Multiplication et division d’entiers relatifs
        a = randint(-12,12)
        b = randint(-12,12)
        if randint(0,1) == 0:  # Multiplication
            text = str(a)+" × "+nb(b)+" = "
            question(text, str(a*b), text)
        else:  # Division
            if b == 0: b = 1
            text = str(a*b)+" ÷ "+nb(b)+" = "
            question(text, str(a), text)

    if quoi == 3:  # Mise sous forme irréductible de fractions
        n = randint(-50,50)
        d = randint(-50,50)
        if d == 0: d = 1
        text = "Réduire "+ str(n)+"/"+str(d) +" : "
        question(text, frac(n, d), str(n)+"/"+str(d) +" = ")

    if quoi == 4:  # Multiplication et division de fractions et d’entiers
        a = randint(-12,12)
        b = randint(-12,12)
        c = randint(-12,12)
        d = randint(-12,12)
        if b == 0: b == 1
        calcul = randint(0,2)
        if calcul == 0:  # Multiplication entier et fraction
            if randint(0,1) == 0:  # a × c/b
                text = str(a)+" × "+nb(c)+"/"+str(b)+" = "
            else:  # a/b × c
                text = str(a)+"/"+str(b)+" × "+nb(c)+" = "
            question(text, frac(a*c, b), text)
        elif calcul == 1:  # a/b × c/d
            if d == 0: d == 1
            text = str(a)+"/"+str(b)+" × "+str(c)+"/"+str(d)+" = "
            question(text, frac(a*c, b*d), text)
        elif calcul == 2:  # a/b ÷ c/d
            if b == 0: b = 1
            if c == 0: c = 1
            text = str(a)+"/"+str(b)+" ÷ "+str(c)+"/"+str(d)+" = "
            question(text, frac(a*d, b*c), text)

    if quoi == 5:  # Simplification racine carrée
        x = randint(4, 100)
        text = "Simplifier √"+str(x)+" : "
        question(text, rac(x), ""+str(x)+" = ")

    if quoi == 6:  # Proportion en %
        p = (5, 10, 20, 25, 30, 40, 50, 60, 70, 75, 80, 90)
        a = p[randint(0, len(p)-1)]
        t = 20 * randint(1, 20)
        text = str(a)+" % de "+str(t)+" : "
        rep = a*t/100
        if rep == int(rep): rep = int(rep)
        question(text, str(rep), str(a/100)+" × "+str(t)+" = ")

    if quoi == 7:  # Coefficient multiplicateur
        p = randint(-100, 100)
        if p == 0: p = 1
        if p > 0:
            text = "+"+str(p)+"% <-> ×"
            aide = " + "+str(p/100)+" = "
        else:
            text = str(p)+"% <-> ×"
            aide = " - "+str(-p/100)+" = "
        rep = round(1 + p/100, 2)
        question(text, str(rep), "1"+aide)

# FIN DU PROGRAMME

print("Bilan : " + str(ok/2) + "/" + str(6))

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.