[Tutor] string list in alphabetical!

Alan Gauld alan.gauld at btinternet.com
Mon Oct 21 11:46:13 CEST 2013


On 21/10/13 01:16, Sammy Cornet wrote:

> so here is what I have on my script:

OK some comments below...

> infile = open('Desktop/unsorted_fruits.docx' ,"r")
> outfile = open('Desktop/sorted_fruits.docx', 'w')

You probably want to use txt files.

> def find():
>      index = 0
>      while index < 26:
>          list < 26

You haven't created list yet so you can't compare it to anything. But 
even if you had this line does nothing useful.

>          list = ["a", "b", "c", "e", "f", "g", "h", "i", "j", "k", "l",
> "m", "n" "o", "p", "q", "r", "s", "t", "u", "v", "w", "z", "y", "z"]

OK, Now you've created list... Although its a bad choice of name because 
it hides the list() operation in Python. So you can't now convert things 
to lists.

>          if index[0] == list[0]:

This will fail since index is a number and list is a list. You can't 
index a number. I'm not sure what you thought you were comparing?

>              infile = list + 1

I don't know what you think this does but what it does in practice is 
throws away your open file and tries to add 1 to your list which is an 
error. You can't add lists and numbers.

>              print infile

>          index += 1
>
>      infile.close()
>      outfile.close()

Since you haven't read anything from infile or written anything to 
outfile this doesn't achieve much.

I think you need to sit down with a pen and paper and work out how you 
would solve this problem then convert that to Python. As it is you have 
a long way to go. I also think you may be making the exercise much 
harder than it should be. Take a look at the documentation for the 
sort() method of lists, that should help.

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list