[Tutor] compare

Johan Geldenhuys johan at accesstel.co.za
Mon Oct 31 11:16:15 CET 2005


In your code that line is not executed. And if it was at some stage, it 
will give you an error.
max_item is a list and you cannot put a string and a list together:  
"TypeError: cannot concatenate 'str' and 'list' objects".

What are trying to do with this line? 'maximum = 0' and so product will 
be greater than maximum. It looks you are trying to append a list to seq 
that is already there?
Maybe it's a bad guess from me, I'm no expert.

Johan

Shi Mu wrote:

>What does this line 11 mean in the following code?
>
>1 # calc.py
>    2 def calc(seq):
>    3   maximum = 0
>    4   max_item = []
>    5   for i in seq:
>    6     product = (i[0]*100 + i[1]*10 + i[2]) * (i[3]*10 + i[4])
>    7     if product > maximum:
>    8        maximum = product
>    9        max_item = i
>   10     elif product == maximum:
>   11        max_item += ','+i
>   12   return max_item, maximum
>   13
>   14 seq = [ [5,6,7,8,9], [5,6,7,9,8] ]
>   15 max_item, maximum = calc(seq)
>   16 print "Maximum at", max_item, ",product", maximum
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor
>
>  
>


More information about the Tutor mailing list