Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

2.3 Inputoutput With Print and Input Functions

Uploaded by

Rahul Sonawane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

2.3 Inputoutput With Print and Input Functions

Uploaded by

Rahul Sonawane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Typing things in Python:

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.

temp = eval(input('Enter a temperature in Celsius: '))


print('In Fahrenheit, that is', 9/5*temp+32)

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:

name = input('Enter your name: ')


print('Hello, ', name)

The basic structure is

variable name = input(message to user)

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:

num = eval(input('Enter a number: '))


print('Your number squared:', num*num)

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.

You might also like