[Tutor] Sorting a list in ascending order

meenu ravi meenuravi89 at gmail.com
Fri Apr 29 16:58:30 EDT 2016


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

On Fri, Apr 29, 2016 at 3:01 PM, Ken G. <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'
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list