dumb newbie questions
nate
nathanhewat at yahoo.com
Thu Jan 17 13:53:41 EST 2002
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
More information about the Python-list
mailing list