dumb newbie questions

Steve Holden sholden at holdenweb.com
Thu Jan 17 15:25:25 EST 2002


"nate" <nathanhewat at yahoo.com> wrote in message
news:5a3612cc.0201171053.5257ce97 at posting.google.com...
> Hi!
>
> I'm trying to teach myself Python using 'Learning Python' by Lutz and
> Ascher.  I hope nobody's offended by the trivial nature of my
> questions.  As you can probably tell, I have no previous coding
> experience and so would appreciate any constructive critisism.
>
> I'm aware that the numeric extension to python probably has everything
> I need, but would prefer to master the basics and learn to code the
> things numPy does, before I use the tools it contains (...and partly
> to brush up on Linear Algebra)
>
> I'm currently stuck (at the beginning, dammit!) on this piece of code
> to manually enter a matrix...
>
> #!/usr/bin/python1.5
> import sys
> print 'enter the no. of rows'
> row=sys.stdin.readline()
> print 'enter the no. of columns'
> col=sys.stdin.readline()
> print 'a',row,'x',col,'matrix has been entered'
> #
> # I want it to print: 'a row x col matrix... '
> # but it prints a newline after each number (row and col).
> # there were no problems with it at the interactive prompt.
> #
> i=1
> j=1
> matrix=[]
> while i<=row:
> ith_row=[]
> while j<=col:
> print 'enter the', i, j, '-th element'
> a=sys.stdin.readline()
> ith_row.append(a)
> j=j+1; continue
> #
> # What am I doing wrong with this loop? - it fails to move on when
> j>col, and
> # returns 'enter the 1 <nth> -th element' to infinity (?!?)
> # I tried using for loops eg: 'for j in range(col):' Why can I not use
> col with
> # range?
> #
> else:
> matrix.append(ith_row)
> i=i+1; break
> else:
> print 'the matrix you have entered is...'
> for rows in matrix:
> print rows
> else:
> print '\nfinished'
>
> I think the rest of it's ok... If you respond to this please be
> careful to say it slow, so I can understand ;o). Cheers

For interactive input you'll find the raw_input() function somewhat easier:

print 'enter the no. of rows'
row = raw_input('enter the no. of columns ')
col = raw_input('a',row,'x',col,'matrix has been entered ')
    ...
Unfortunately you can't just say:

a = raw_input('enter the', i, j, '-th element ')
    ...
because raw_input only expects a single string, so you have to build the
string you want to use as a prompt. So you would say:

a = raw_input('enter the (%s,%s)-th element ' % (i, j))

This puts i and j instead of the two %s bits - the "%" operator can be used
for all sorts of string substitutions, but you don't need all the details
yet.

You would find for loops much easier. The problems you were having were
related to the "col" and "row" variables holding strings rather than
numbers. You can fix this by converting them:

row = int(row)
col = int(col)

With these changes your input code would become:

matrix = []
for i in range(row):
    ith_row = []
    for j in range(col):
        a = raw_input('enter the (%s,%s)-th element ' % (i, j))
        ith_row.append(a)
    matrix.append(ith_row)

Hope this was slow enough. You seem to have grasped most of the essentials -
I guessed you had at least some experience in another programming language,
so maybe you're doingt better than you think. Your output code looks OK. See
how it goes with these changes.

regards
 Steve
--
Consulting, training, speaking: http://www.holdenweb.com/
Python Web Programming: http://pydish.holdenweb.com/pwp/








More information about the Python-list mailing list