Défi

L'objectif est de réaliser un programme pour un distributeur de billets de banque.

Question

Écrire un programme ayant une boucle infinie qui affiche ce menu à l'utilisateur :

1. Retrait 10 €

2. Retrait 20 €

3. Retrait 50 €

4. Retrait 100 €

5. Autre montant

6. Quitter

Ajouter la structure conditionnelle nécessaire pour sortir de la boucle si l'utilisateur entre « 6 ».

Indice

Simuler une boucle infinie avec while et un booléen. Ce booléen restera à true tant que l'utilisateur n'aura pas entré le nombre 6.

Indice

On demande une chaîne de caractères à l'utilisateur avec la fonction input().

Solution

1
stay_in_loop = True
2
3
while stay_in_loop:
4
    print("============================")
5
    print("Choisir une option")
6
    print("1. Retrait 10€")
7
    print("2. Retrait 20€")
8
    print("3. Retrait 50€")
9
    print("4. Retrait 100€")
10
    print("5. Autre montant")
11
    print("6. Quitter")
12
    user_input = input()
13
14
    if user_input == "6":
15
        stay_in_loop = False

stay_in_loop permet de dire à la boucle que l'on souhaite y rester. Lorsque l'utilisateur entre 6, stay_in_loop passe à False.

Question

Compléter la structure conditionnelle pour demander à l'utilisateur quel montant il souhaite retirer quand il entre « 5 ».

Indice

Le mot-clé elif permet de définir une alternative de type sinon si.

Solution

1
stay_in_loop = True
2
3
while stay_in_loop:
4
    print("============================")
5
    print("Choisir une option")
6
    print("1. Retrait 10€")
7
    print("2. Retrait 20€")
8
    print("3. Retrait 50€")
9
    print("4. Retrait 100€")
10
    print("5. Autre montant")
11
    print("6. Quitter")
12
    user_input = input()
13
14
    if user_input == "5":
15
        amount = input("Veuillez choisir un montant : ")
16
    elif user_input == "6":
17
        stay_in_loop = False

Question

Compléter la structure conditionnelle pour afficher « Vous avez retiré x € » avec « x » le montant retiré.

Indice

print() permet d'afficher des chaînes de caractères sur la console.

Solution

1
stay_in_loop = True
2
3
while stay_in_loop:
4
    print("============================")
5
    print("Choisir une option")
6
    print("1. Retrait 10€")
7
    print("2. Retrait 20€")
8
    print("3. Retrait 50€")
9
    print("4. Retrait 100€")
10
    print("5. Autre montant")
11
    print("6. Quitter")
12
    user_input = input()
13
14
    if user_input == "1":
15
        print("Vous avez retiré 10€")
16
    elif user_input == "2":
17
        print("Vous avez retiré 20€")
18
    elif user_input == "3":
19
        print("Vous avez retiré 50€")
20
    elif user_input == "4":
21
        print("Vous avez retiré 100€")
22
    elif user_input == "5":
23
        amount = input("Veuillez choisir un montant : ")
24
        print("Vous avez retiré " + amount + "€")
25
    elif user_input == "6":
26
        stay_in_loop = False

Question

Terminer la structure conditionnelle pour gérer le cas où l'utilisateur se trompe dans sa sélection au menu.

Indice

L'alternative par défaut (sinon) s'écrit else.

Solution

1
stay_in_loop = True
2
3
while stay_in_loop:
4
    print("============================")
5
    print("Choisir une option")
6
    print("1. Retrait 10€")
7
    print("2. Retrait 20€")
8
    print("3. Retrait 50€")
9
    print("4. Retrait 100€")
10
    print("5. Autre montant")
11
    print("6. Quitter")
12
    user_input = input()
13
14
    if user_input == "1":
15
        print("Vous avez retiré 10€")
16
    elif user_input == "2":
17
        print("Vous avez retiré 20€")
18
    elif user_input == "3":
19
        print("Vous avez retiré 50€")
20
    elif user_input == "4":
21
        print("Vous avez retiré 100€")
22
    elif user_input == "5":
23
        amount = input("Veuillez choisir un montant : ")
24
        print("Vous avez retiré " + amount + "€")
25
    elif user_input == "6":
26
        stay_in_loop = False
27
    else:
28
        print("Mauvaise entrée")