The problem here is that these are home work assignment and to code an else word assignment would be in contradiction to the academic curriculum.
Specifically, I want to deploy a quick and simply dic solution: Revised code: c = [0 for i in range(24)] filename = raw_input("Enter file name: ") f = open(filename,'r') for l in f: h = int(l.strip().split()[X].split(':')[Y]) c[h] = c[h] + 1 f.close() for i in range(24): print '{:02d} {}'.format(i, c[i]) --------------------------------------------------------------------------- NameError Traceback (most recent call last) C:\Users\vm\Desktop\apps\docs\Python\week_10\assignment_10_2_v_10.py in <module> () 5 f = open(filename,'r') 6 for l in f: ----> 7 h = int(l.strip().split()[X].split(':')[Y]) 8 c[h] = c[h] + 1 9 f.close() NameError: name 'X' is not defined In [74]: print X --------------------------------------------------------------------------- Print X: NameError Traceback (most recent call last) <ipython-input-74-7510f249b93c> in <module>() ----> 1 print X NameError: name 'X' is not defined In [75]: On Wed, Aug 12, 2015 at 2:25 PM, Gene Heskett <ghesk...@wdtv.com> wrote: > On Wednesday 12 August 2015 15:05:37 Ltc Hotspot wrote: > >> >Have a look at assignment_10_2_v_06.py. >> >> What should I look at assignment_10_2_v_06.py.: >> >> >> handle = """From stephen.marqu...@uct.ac.za Sat Jan 5 09:14:16 2008 >> From lo...@media.berkeley.edu Fri Jan 4 18:10:48 2008 >> """.split("\n") >> # Snippet file data: mbox-short.txt >> >> count = dict() >> #fname = raw_input("Enter file name: ")# Add Snippet file >> #handle = open (fname, 'r')# Add Snippet file >> for line in handle: >> if line.startswith("From "): >> time = line.split() # Sort time >> >> hours = line.split(":")[5] # Sort hours >> line = line.rstrip() >> >> count[hours] = count.get(hours, 0) + 1 # counter >> >> lst = [(val,key) for key,val in count.items()] >> >> print key, val >> >> On Wed, Aug 12, 2015 at 11:59 AM, MRAB <pyt...@mrabarnett.plus.com> > wrote: >> > On 2015-08-12 19:35, Ltc Hotspot wrote: >> >> Emile >> >> >> >> How do I define time in the revised code ? >> > >> > Have a look at assignment_10_2_v_06.py. >> > >> >> ------------------------------------------------------------------- >> >>-------- Traceback Message reads: >> >> In [66]: %run assignment_10_2_v_07 >> >> >> >> NameError >> >> Traceback (most recent call last) >> >> C:\Users\vm\Desktop\apps\docs\Python\week_10\assignment_10_2_v_07.p >> >>y in <module> >> >> () >> >> 9 for line in handle: >> >> 10 if line.startswith("From "): >> >> ---> 11 for key, val in time.split()[5]: >> >> 12 for key, val in hours.split(':')[0]: >> >> 13 count[hours] = count.get(hours, 0) + 1 # >> >> counter >> >> >> >> NameError: name 'time' is not defined >> >> >> >> ------------------------------------------------------------------- >> >>-------- >> >> >> >> >> >> In [67]: print time >> >> 0 >> >> >> >> ------------------------------------------------------------------- >> >>-------- >> >> >> >> Revised Code reads: >> >> handle = """From stephen.marqu...@uct.ac.za Sat Jan 5 09:14:16 >> >> 2008 >> >> >> >>> From lo...@media.berkeley.edu Fri Jan 4 18:10:48 2008 >> >> >> >> """.split("\n") >> >> # Snippet file data: mbox-short.txt >> >> >> >> count = dict() >> >> #fname = raw_input("Enter file name: ")# insert snippet file >> >> #handle = open (fname, 'r')# insert snippet file >> >> for line in handle: >> >> if line.startswith("From "): >> >> for key, val in time.split()[5]: >> >> for key, val in hours.split(':')[0]: >> >> count[hours] = count.get(hours, 0) + 1 # counter >> >> >> >> lst = [(val,key) for key,val in count.items()] >> >> lst.sort(reverse=True) >> >> >> >> for key, val in lst[:12] : >> >> print key, val >> >> >> >> >> >> ------------------------------------------------------------------- >> >>-------- Regards, >> >> Hal >> > >> > I'm baffled as to why you now have a nested 'for' loop 3 levels >> > deep! >> > >> > -- >> > https://mail.python.org/mailman/listinfo/python-list > > After following this thread to late afternoon today, I would say that > Denis's response was a bit strong and less than helpfull because the > perceived insult drowns out the true meaning of his remark. > > OTOH, _you_ are putting zero effort into understanding the error, > something the others have encouraged, apparently several times now on at > least 2 other venues. > > This IS how one learns best, by seeing an error and taking an action that > aleviates the error. Handing the answer to you on a clean plate may fix > the error, but your chances of understanding why it was fixed are > perhaps 10% as good than the understanding in the future would be if you > followed normal debugging proceedures and fixed it yourself. > > If you aren't willing to put any effort into it yourself, then perhaps > Denis does have the right idea. Find another line of endeavor that > _will_ pay the bills. > > Cheers, Gene Heskett > -- > "There are four boxes to be used in defense of liberty: > soap, ballot, jury, and ammo. Please use in that order." > -Ed Howdershelt (Author) > Genes Web page <http://geneslinuxbox.net:6309/gene> > -- > https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list