[Tutor] Alice_in_wonderland Question

Brian van den Broek brian.van.den.broek at gmail.com
Mon May 5 05:35:23 CEST 2014


On May 4, 2014 11:13 PM, "Jake Blank" <jakenblank at gmail.com> wrote:
>
> To figure that last part out I just did a simple if statement.
> for k in sorted(word_count, key=lambda x:word_count[x], reverse=True):
>         if word_count[k] >=300:
>                 print (k, word_count[k])
> And the output was correct.

Jake,

That was in an effort to get only the 15most frequent words, if I
understood your earliest post aright. If so, even if the output was correct
for the case, that is by accident. What happens in only 3 distinct words
show up 300 times? If 42 do?

You need to rethink, I think. Do you know how list indexing and list
slicing work?

> I did have one more question though.
>
> import os
> from wordtools import extract_words
>
> source_filepath=input("Enter the path to the source file:")
> dest_filepath =input("Enter the path to the destination file:")
>
> sourcef=open(source_filepath, 'r')
> destf=open(dest_filepath, 'w')
> for line in sourcef:
>         destf.write(line)
> file=input ("Would you like to process another file?(Y/N):")
> if file== "Y":
>     source_filepath=input("Enter the path to the source file:")
>     dest_filepath =input("Enter the path to the destination file:")
> else:
>
> This code asks the user for a source/dest_filepath.
> I'm wondering how I can make it so the program can tell if the
source/dest_filepath the user entered is actually a program on the computer.

" a program on the computer"? I assume that you mean file.

What happens if you run

open(" nowayyouhaveafilecalledthiscausethatdbesilly", 'r')

That's rather different than if the path is the path of an existing file,
right. So, you could try to react differently to the two sorts of results.

Do you know how to use try an except?

> Also i have to ask the user if they would like to "process another
file(Y/N)?" and I'm not sure where to put that.

I shall leave this for others; I'm not ready for my Monday, yet.

Best,

Brian vdB
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140504/b1a14e38/attachment-0001.html>


More information about the Tutor mailing list