LESSON 06
Getting User Input
Until now your programs talked. Now they listen.
USES: print( ), variables, strings, f-strings, len( ).
01
Programs that ask
Every program so far does the same thing every time you run it. The name is written inside the code, so to change it you have to edit the code. Real programs ask the person using them. Python's tool for that is a function called input( ). When Python reaches input( ), it pauses, waits for the person to type something, and continues when they press Enter.
BLUEPRINT ยท TAP OR HOVER EACH PIECE
Blueprint 1 of 1: the input. Tap each piece.
02
The prompt is just a string
The text inside the brackets is the question the person sees. It's an ordinary string. Tip: put a space at the end of the prompt, inside the quotes. Without it, the person's typing starts right against the question mark.
03
The catch: everything is text
Here is the surprise. Try asking for a number. If the person types 25, and you try to add 10, Python crashes. Why? Because input( ) ALWAYS gives you text (a string). It doesn't know you wanted a number. It treats '25' as the letters '2' and '5', not the number twenty-five.
04
The silent bug
Sometimes it doesn't crash, it just does the wrong thing. If you ask for two numbers and add them, Python glues the text together instead of doing maths. '5' + '3' becomes '53', not 8. This is the same trap from Lesson 4.
05
Quick quiz: what does this show?
The person types 4. What does this print? x = input("Number: ") print(x * 3)