[Tutor] multidemisional arrays
Jacob S.
keridee at jayco.net
Fri Nov 19 03:13:00 CET 2004
I'm surprised that nobody has corrected the excessive code involved in the
first loop.
>i=0
>howMany=input('How many artists will you enter? ')
>paintings=[]#painting
>artists=[]#artist
>values=[]#value
>for i in range(0,howMany,1):
> painting=raw_input('Painting Name ')
> artist=raw_input('Artist Name ')
> value=input('Painting Value ')
> paintings+=[painting]
> artists+=[artist]
> values+=[value]
> artists=zip(artists,paintings,values)
> artists.sort()
> i=i+1
You do not need to define i as 0, you do not need to increment i at the end
of loop, that is what
for i in range(0,howMany,1): means.
Also, you can shorten that to: for i in range(howMany):
>n=0
>for n in range(0,howMany,1):
> print artists[n]
If you want to figure this out as a learning experience, DO NOT READ ANY
FURTHER!!!
I would do this.
def rfill(stri,length):
if len(stri) < length:
stri = stri+' '*(length-len(stri))
return stri
howMany = input('How many artists will you enter? ')
artists = {}
for i in range(howMany):
artist = raw_input('Who is the artist? ')
painting = raw_input('What is the painting called? ')
value = raw_input('What is the value of the painting? ')
artists[artist] = (painting,value)
artists.keys().sort()
l = 15
a = rfill("Artist",l)
p = rfill("Painting",l)
v = rfill("Value",l)
print "".join([a,p,v])
for x in artists.keys():
a = rfill(x,l)
p = rfill(artists[x][0],l)
v = rfill(artists[x][1],l)
print "".join([a,p,v])
More information about the Tutor
mailing list