Python 2.4: Requesting data from the user
Cette vidéo introduit la fonction input en Python, qui permet de demander une saisie à l'utilisateur et de récupérer sa réponse. Jusqu'ici, le programme affichait juste un nom écrit en dur dans le code. Avec input, on peut interagir avec l'utilisateur et adapter l'affichage à sa réponse.
Syntaxe et comportement de input
La fonction input reçoit en argument un message d'invite qui sera affiché à l'écran, et retourne la chaîne saisie par l'utilisateur. Le programme se bloque sur cette ligne jusqu'à ce que l'utilisateur tape sa réponse et appuie sur Entrée :
nom = input("Quel est ton nom ? ")
print("Bonjour " + nom)
Petite astuce de mise en forme : ajouter un espace après le point d'interrogation dans le message d'invite, sinon la réponse se colle juste après. "Quel est ton nom ? " avec l'espace est nettement plus lisible que "Quel est ton nom?" qui colle le curseur à la question.
Plusieurs points importants à retenir sur input :
- La fonction retourne toujours une chaîne de caractères (str), même si l'utilisateur tape un nombre
- Pour récupérer un entier, il faut convertir :
age = int(input("Ton âge ? ")) - Si la conversion échoue (saisie non numérique), Python lève une ValueError
- Le programme reste bloqué tant que l'utilisateur n'a pas appuyé sur Entrée
- La variable doit être déclarée AVANT d'être utilisée (ordre : déclaration puis appel)
L'intérêt fondamental des variables, c'est de manipuler des données qu'on ne connaît pas à l'avance. Avec un nom écrit en dur, le programme dit toujours "Bonjour Charles". Avec input, il s'adapte : "Bonjour Alice" si l'utilisateur tape Alice, "Bonjour Bob" si c'est Bob. C'est la base de tout programme interactif. La prochaine vidéo proposera un exercice pour mettre cela en pratique.
Summary
This lesson introduces the `input()` function in Python, which enables requesting data directly from users during program execution. The function pauses program execution, displays a prompt to the user, and returns the entered data which can be stored in a variable. The lesson emphasizes the importance of declaring variables before using them, and demonstrates how variables allow programs to work with data that can change at runtime.
Key points
- The `input()` function retrieves user input and stores it in a variable for further manipulation
- The `input()` function is blocking—the program pauses and waits for the user to type and press Enter before continuing
- Variables must be declared before they are used; attempting to use an undeclared variable causes an error
- Variables are essential for manipulating data that is not known beforehand and may change during program execution
- Variable declaration and variable usage are two separate steps that must happen in order
FAQ
What does the `input()` function do?
The `input()` function displays a message to the user, waits for the user to type something and press Enter, and returns the entered value. This value can be stored in a variable for later use in the program.
Why is the order of variable declaration and use important?
Variables must be declared first before they can be used. If you try to use a variable before declaring it, Python will raise an error because the variable does not exist yet.
When is the `input()` function useful?
The `input()` function is useful when you need to request information from the user at runtime—data that is unknown beforehand and may change each time the program runs. Variables store this data so it can be processed and manipulated throughout the program.