classification
Title: Max() Not Working Properly
Type: Stage:
Components: IDLE Versions: Python 3.1
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, lovelygoo2
Priority: normal Keywords:

Created on 2009-09-21 03:03 by lovelygoo2, last changed 2009-09-21 03:14 by benjamin.peterson. This issue is now closed.

Messages (2)
msg92920 - (view) Author: (lovelygoo2) Date: 2009-09-21 03:03
>>> max(['34', '7'])
'7'
>>> #This happens because it is comparing strings.
>>> #I have found a way to fix it though.
>>> def Max(li):
...    greatest=li[0]
...    for item in li:
...        for item2 in li:
...            if int(item)<int(item2) or int(item)<greatest:
...                break
...            else:
...                greatest=int(item)
...    return(greatest)
msg92921 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2009-09-21 03:14
This is the way strings are compared. You can change this easily,
though, using max(list_of_string_numbers, key=int).
History
Date User Action Args
2009-09-21 03:14:58benjamin.petersonsetstatus: open -> closed

nosy: + benjamin.peterson
messages: + msg92921

resolution: wont fix
2009-09-21 03:03:04lovelygoo2create