2.3 Inputoutput With Print and Input Functions
2.3 Inputoutput With Print and Input Functions
Case: Case matters. To Python, print, Print, and PRINT are all different things. For now, stick
with lowercase as most Python statements are in lowercase.
Spaces: Spaces matter at the beginning of lines, but not elsewhere. For example, the code below
will not work.
Spaces in most other places don’t matter. For instance, the following lines have the same effect:
print('Hello world!')
print ('Hello world!')
print( ' Hello world!' )
Basically, computers will only do what you tell them, and they often take things very literally.
Python itself totally relies on things like the placement of commas and parentheses so it knows
what’s what. It is not very good at figuring out what you mean, so you have to be precise. It will
be very frustrating at first, trying to get all of the parentheses and commas in the right places, but
after a while it will become more natural. Still, even after you’ve programmed for a long time,
you
will still miss something. Fortunately, the Python interpreter is pretty good about helping you
find
your mistakes.
Getting input:
The input function is a simple way for your program to get information from people using your
program. Here is an example:
The above works for getting text from the user. To get numbers from the user to use in
calculations,
we need to do something extra.
Here is an example:
The eval function converts the text entered by the user into a number. One nice feature of this is
you can enter expressions, like 3*12+5, and eval will compute them for you.
Note If you run your program and nothing seems to be happening, try pressing enter. There is a
bit of a glitch in IDLE that occasionally happens with input statements.