Pages

Friday, 7 June 2013

[4/??] basic Python

go to 12, 3, 4



Python: script file, formatted output, input from keyboard


now it is enough to make script file. it is a file that contains many statement to be executed at a time.

how to create:

just click 'New Window' (ctrl+n)


a new black window will appear like this.
then you can start type.



let's try something.

very simple, right ?

then push the key F5 to run it ( i saved it on my desktop, should be saved before run it )
note that after '#', anything will be ignored. conventionally, there will be comment, explanation, etc... about the program



here shell window~
there it is.

but the output looks kind of messy




so i edited something using formatted print statement

now, looks better

% means that there will be format codes
examples,

%d or %i - signed integer
%e or %E - floating point with exponent
%f or %F - floating point without exponent
%g or %G - floating point general..but use %e or %f instead.
%c - character
%s - string (str())
%r - string (repr())

these would be really useful in certain situation.
so memorize them






input from user keyboard

by far, we just did output statement
but since now we will deal with input



i edited some with values' assignments
raw_input('prompt')   will ask you to enter something on shell window (IDLE) when it is executed


like this
type a number each line, then press Enter key to process




but !
there will be an error at last ( related with date type )

we can easily fix this thing.

actually, the statement we used, raw_input() will cast the value as type of string.
so Python can not calculate strings

to fix this, just add int() around the raw_input statement.
like this
value1,2,3 will accept those input values as integer so that total and avg variables can calculate them





now totally fixed the error.

you can do float() to cast as floating point number(real number) if there will be real number.

No comments:

Post a Comment