0% ont trouvé ce document utile (0 vote)
9 vues4 pages

Algorithmes de base en Python

Le document présente des algorithmes et leur implémentation en Python pour diverses opérations, notamment le calcul de la somme de deux entiers, la résolution d'équations du second degré et la conversion de minutes en secondes. Il inclut des exemples de code Python pour chaque algorithme, illustrant la syntaxe et les étapes nécessaires. Des exercices conditionnels sont également fournis pour évaluer les notes des étudiants.

Transféré par

test168458
Copyright
© All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats ODP, PDF, TXT ou lisez en ligne sur Scribd
0% ont trouvé ce document utile (0 vote)
9 vues4 pages

Algorithmes de base en Python

Le document présente des algorithmes et leur implémentation en Python pour diverses opérations, notamment le calcul de la somme de deux entiers, la résolution d'équations du second degré et la conversion de minutes en secondes. Il inclut des exemples de code Python pour chaque algorithme, illustrant la syntaxe et les étapes nécessaires. Des exercices conditionnels sont également fournis pour évaluer les notes des étudiants.

Transféré par

test168458
Copyright
© All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats ODP, PDF, TXT ou lisez en ligne sur Scribd

Introduction Algorithme&Python

Il s’agit de calculer et d’afficher la somme de deux entiers


Somme = a+b

ALGO : PYTHON : FORME GENERALE PYTHON:

algorithme ex1
a=int(input(‘’saisie a :’’) a =int(input("Tapez la valeur du nombre a : "))
Debut b =int(input("Tapez la valeur du nombre b : "))
b=int(input(‘’saisie b :’’)
# Convertir les chaines de caractères en entier
lire(a)
s=a+b s = a+b
lire(b) # afficher le résulatat de la somme a + b
print(‘’la somme de
somme←a+b a+b=’’,s) print("La somme de a et de b est a + b = " , s)

Ecrire(somme)

fin
Resoulition equation second degre avec
python
from math import* if delta<0 :

print("Resoulition De L'equation de print("L'équation ax 2 + bx + c = 0 n'a pas de solution réelle")


second degre ax**2+bx+c")
elif delta == 0 :
a=int(input("saisir la va leur de a "))
x0= -(b/(2*a))
b=int(input("saisir la va leur de b "))
print("L'équation ax2 + bx + c = 0 a une unique solution : x0 = −b/2a=",x0)
c=int(input("saisir la va leur de c "))
else :
print("forme de
trinome",a,"x**2+",b,"x+",c,"=0") x1= (-b - sqrt(delta))/(2*a)

delta= b**2-4*(a*c) x2= (-b + sqrt(delta))/(2*a)

print("DELTA=",delta) print("les racines x1=",x1)

print("les racines x2=",x2)


EXERCICES IF
note = float ( input (" saisir une note :" ))
if note <10 :
print ( " non admis ")
elif note <12 :
print ( " passable ")
elif note <14 :
print ( " assez bien ")
elif note <16 :
print ( " bien ")
elif note <=20 :
print ( " tres bien ")
else :
print ( " saisir une note valide ")
EXERCICES STRUCTURE D
Convertir un nombre n1 (qui correspond au nombre de minutes) en nombre n2 qui
correspond en nombre de secondes sachant que n1 est une donnée à entrer par l’utilisateur.

Algorithme PYTHON
Algorithme ex3 n=int(input(‘’saisir n1=’’ ‘’min’’))
n2=n1*60
lire(n1) print(‘’convertion de n1 en seconde
n2←n1*60 n2=’’ ,n2, ‘’s’’)

Ecrire(n2)
fin

Common questions

Alimenté par l’IA

The error handling mechanism for invalid inputs in the student performance categorization program is rudimentary. It checks whether an input score exceeds 20 and prompts the user to 'saisir une note valide,' which helps mitigate typing errors. However, it lacks robustness as it does not handle non-numeric inputs, negative scores, or particularly out-of-range values not explicitly defined. More robust error handling would include try-except blocks that catch ValueErrors or other exceptions, ensuring that only valid numeric inputs are processed and offering comprehensive user feedback .

Conditional statements in Python are used to evaluate a student's performance by categorizing their inputted score into different performance tiers. Depending on the score range, different outputs are printed: less than 10 results in 'non admis', less than 12 is 'passable', 12 to less than 14 is 'assez bien', 14 to less than 16 is 'bien', and 16 to 20 is 'tres bien'. If the score exceeds 20, an error message is issued, prompting for a valid score. These conditional logic statements help in decision-making processes by defining specific actions based on conditions .

Delta, when solving a second-degree (quadratic) equation, is significant because it dictates the nature and number of solutions. It is calculated as delta = b^2 - 4ac. If delta is less than zero, the equation has no real solutions. If delta equals zero, there is exactly one real solution, which is a repeated root. If delta is greater than zero, the equation has two distinct real solutions. Hence, determining delta is crucial for identifying how many and what types of roots a quadratic equation will have .

The "math" module is used in the Python code example for solving a second-degree equation to access mathematical functions such as 'sqrt' (square root), which is necessary for calculating the roots of the equation when solving ax^2 + bx + c = 0. Specifically, the 'sqrt' function is used when computing the square root of delta (b^2 - 4ac) to determine the roots x1 and x2 when delta is greater than zero .

The algorithm for calculating the sum of two integers in Python involves several steps: First, the program prompts the user to input two integers. It then converts these string inputs into integers. Subsequently, it calculates their sum by adding the two integers. Finally, it prints the result of the sum. This step-by-step approach ensures the input values are properly handled and the computation executed correctly .

Python handles user input by using the input() function to prompt users to enter values, which are then read as strings. To solve a quadratic equation, these inputs (coefficients a, b, and c) must be converted to integers using the int() function because mathematical operations require numeric data types. This conversion facilitates further computations such as calculating delta and solving the equation. By processing user inputs this way, the program can dynamically handle different sets of coefficients and solve the equation accordingly .

The algorithm for converting minutes into seconds in Python involves multiplying the number of minutes by 60. The user inputs the number of minutes as a variable n1, and the resulting number of seconds is calculated by setting n2 equal to n1 multiplied by 60. This conversion process is straightforward because the relationship between minutes and seconds is constant at 60 seconds per minute .

Print statements play a crucial role in debugging Python code by allowing programmers to output variable values and computational results at different stages of execution. By displaying intermediary and final results, developers can verify that each part of the algorithm functions as intended. This checks the flow of data, helps spot logical errors, and ensures correct execution of algorithms. In the given examples, print statements confirm input values, computation results such as delta, and the roots of quadratic equations, thus validating correct code behavior .

The document demonstrates implementing a basic algorithm in Python through step-by-step examples. It starts with user prompts, reads input values, converts necessary data types, performs computations like addition or solving equations, and finally outputs results using print statements. These examples, including calculating a sum or solving quadratic equations, illustrate the importance of structuring code for clarity and functionality, emphasizing input handling, process logic, and consistent output formats .

To enhance the provided Python scripts, several improvements could be made. Adding comprehensive input validation and error handling through try-except blocks would prevent crashes from invalid inputs. Implementing functions to separate logic from user interaction would increase modularity and readability. Additionally, using more detailed error messages and possibly implementing a loop for repeated attempts upon entry errors could improve user experience. Optimizing calculations, such as avoiding recalculating delta when solving quadratic equations, could also improve performance .

Vous aimerez peut-être aussi