[Tutor] Sorting a list in ascending order [RESOLVED]

Ken G. beachkidken at gmail.com
Fri Apr 29 18:58:37 EDT 2016


> On Fri, Apr 29, 2016 at 3:01 PM, Ken G. <beachkidken at gmail.com 
> <mailto:beachkidken at gmail.com>> wrote:
>
>     In entering five random number, how can I best sort
>     it into ascending order, such as 0511414453? Using
>     Linux 2.7.6 in Ubuntu 14.04.4. Thanks.
>
>     number01 = "41"
>     number02 = "11"
>     number03 = "05"
>     number04 = "53"
>     number05 = "44"
>     line = number01 + number02 + number03 + number04 + number05
>     print
>     print line
>     line.sort()
>     print
>     print line
>
>
>     4111055344
>
>     Traceback (most recent call last):
>       File "Mega_Millions_01_Tickets_Entry_TEST_TEST.py", line 11, in
>     <module>
>         print line.sort()
>
>     AttributeError: 'str' object has no attribute 'sort'
>

On 04/29/2016 04:58 PM, meenu ravi wrote:
> Hi Ken,
>
> As the message clearly says, the string object doesn't have an 
> attribute "sort". You are trying to join the inputs as a single 
> string,"line" and then you are trying to sort it. But, as you want the 
> string in the sorted format, you should sort it first and then convert 
> it into string, as follows.
>
> number01 = "41"
> number02 = "11"
> number03 = "05"
> number04 = "53"
> number05 = "44"
> list1 = [number01,number02,number03,number04,number05] # Creating a 
> list with the inputs
>
> list1.sort() # sorting the input
> print ''.join(list1) #making it again as a single string
>
> Hope this helps. Happy coding.
>
> Thanks,
> Meena

Thanks, Meena, that is great! I changed your last line to:

     print "".join(list1)  and it came out as below:

     0511414453

Another quotation mark was needed.

Again, thanks.

Ken




I appreciate all of everybody help.

Ken


More information about the Tutor mailing list