Re: wordcount.py provided exercise solution not working?
Ludo Joris <[email protected]>
| Newsgroups | gmane.comp.python.tutor |
|---|---|
| Message-ID | <[email protected]> |
Hi Matt, You already have a text file, yoyr Python script. Do this: ./wordcount.py --topcount wordcount.py # 14 = 12 for 10 the 9 and 7 word 7 a 6 def 5 in 5 if 5 word_count 4 utility 3 word/count 3 dict 3 this 3 count 3 line 3 first 3 word_count[word] 3 option 3 ----- On Sep 21, 2023, at 1:33 AM, Matthew Borkenhagen <[email protected]> wrote: > Thank you Mats, that is very helpful. Is a file provided by Google or will > I need to create one myself? (Just use a text editor like Notepad++? Save > it with a .txt extension?) > Matt Borkenhagen > On Wed, Sep 20, 2023, 5:10 PM Mats Wichmann <[email protected]> wrote: > > On 9/20/23 16:41, Matthew Borkenhagen wrote: > > > Hello, > > > In working through Google's Python Class ( > > > https://developers.google.com/edu/python) I just ran the provided > > solution > > > from Google for the exercise on dicts and files: wordcount.py > > > When I run this program while in the proper directory at the command > > > prompt, the program only prints *this result*: > > > *usage: ./wordcount.py {--count | --topcount} file* > > > My question is this--Is this provided solution ever reading a file? > > (What > > > is the filename that it should be reading called? Where would this file > > > be located? In the directory google-python-exercises\basic\solution or > > > would I need to create a file myself for it to read in order to test the > > > program?) > > The filename is whatever you tell it. But, apparently, you *have* to > > select one of the options too (it's not "optional"). > > def main(): > > if len(sys.argv) != 3: > > print('usage: ./wordcount.py {--count | --topcount} file') > > sys.exit(1) > > option = sys.argv[1] > > filename = sys.argv[2] > > This says: if the script is not called with three arguments, print an > > error message and quit. By convention, the name of the program you just > > ran is sys.argv[0], so you have to supply two more arguments on the > > command line. The first will be the option you selected, and the second > > will be the filename to count the words in. > > _______________________________________________ > > Tutor maillist - [email protected] > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > _______________________________________________ > Tutor maillist - [email protected] > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - [email protected] To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor