Bonjour à tous !
Je débute en Python et en programmation tout court d'ailleurs, et j'aurais bien besoin de votre expérience !
J'ai chercher pendant de longues heures une solution à mon problème mais je ne vois pas comment le résoudre.
Je cherche à exécuter une instruction si une variable "choixNb" n'est pas un nombre (message d'erreur).
Voici mon code de base qui normalement fonctionne plutôt bien tant que je n'ajoute pas cette instruction :
J'ai essayé en ajoutant à la première condition un elif :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92 import os def ArabicToRoman(): choixNb = int(input("Saisissez un nombre entre 1 et 4999 : ")) arabicToRoman = "" nbRomainMilliers = "" nbRomainCentaines = "" nbRomainDizaines = "" nbRomainUnites = "" if choixNb >= 1 and choixNb <= 4999: milliers = choixNb//1000 if(milliers): for millers in range(milliers): nbRomainMilliers = nbRomainMilliers + "M" choixNb = choixNb - (milliers * 1000) centaines = choixNb//100 if(centaines): if (centaines == 9): nbRomainCentaines = "CM" elif(centaines <= 3): for centaines in range(centaines): nbRomainCentaines = nbRomainCentaines + "C" elif(centaines == 4): nbRomainCentaines = nbRomainCentaines + "CD" elif(centaines > 4): nbRomainCentaines = nbRomainCentaines + "D" centaines = centaines - 5 for centaines in range(centaines): nbRomainCentaines = nbRomainCentaines + "C" centaines = choixNb//100 choixNb = choixNb - (centaines * 100) dizaines = choixNb//10 if(dizaines): if(dizaines == 9): nbRomainDizaines = "XC" elif (dizaines <= 3): for dizaines in range(dizaines): nbRomainDizaines = nbRomainDizaines + "X" elif(dizaines == 4): nbRomainDizaines = nbRomainDizaines + "XL" elif(dizaines > 4): nbRomainDizaines = nbRomainDizaines + "L" dizaines = dizaines - 5 for dizaines in range(dizaines): nbRomainDizaines = nbRomainDizaines + "X" dizaines = choixNb//10 choixNb = choixNb - (dizaines * 10) if(choixNb): if(choixNb == 9): nbRomainUnites = "IX" elif(choixNb <= 3): for choixNb in range(choixNb): nbRomainUnites = nbRomainUnites + "I" elif(choixNb == 4): nbRomainUnites = nbRomainUnites + "IV" elif(choixNb > 4): nbRomainUnites = nbRomainUnites + "V" choixNb = choixNb - 5 for choixNb in range(choixNb): nbRomainUnites = nbRomainUnites + "I" elif(choixNb < 1 or choixNb > 4999): print("Soit votre nombre est nul ou négatif, soit il est supérieur à 4999. Réessayez !") arabicToRoman = (nbRomainMilliers + nbRomainCentaines + nbRomainDizaines + nbRomainUnites) print(arabicToRoman) ArabicToRoman() ArabicToRoman() ArabicToRoman() ArabicToRoman() ArabicToRoman() ArabicToRoman() ArabicToRoman() ArabicToRoman() os.system("pause")
Ça ne fonctionne pas. Si l'utilisateur entre des lettres le programme plante toujours...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 elif(choixNb < 1 or choixNb > 4999): print("Soit votre nombre est nul ou négatif, soit il est supérieur à 4999. Réessayez !") elif type(choixNb)!= int: print("Ceci n'est pas un nombre, veuillez saisir un nombre entre 1 et 4999 : ")
J'ai essayé aussi avec Try: et Exept: mais je ne pense pas les utiliser correctement car pour le coup : si l'utilisateur entre des lettres il y a bien un message d'erreur et le programme boucle sur l'input() mais tout le reste du programme ne fonctionne plus....
Pouvez-vous me filer un coup de main svp ?![]()
Partager