Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Testing First Steps With Testing Writing and Running Doctests

Trouble in running doctest

Hello friends i want to know where i'm doing wrong in my code.i write the doctest for name_length and after that when i ran it with python -m doctest script.py then it will run the program.so please tell me how can i write the test for name_length.the work of the name_length is to return the length of the longest length string in the argument.

import re
import os
class Table:
  def start():
    name=[]
    email=[]
    phone=[]
    job=[]
    twitter=[]
    names=open('A:\\vikaspal\\names.txt','r')
    data=names.read()
    names.close()
    line=re.compile(r'''
      ^(?P<name>[-\w ]*,\s[-\w ]+)\t
      (?P<email>[-\w\d.+]+@[-\w\d.]+)\t
      (?P<phone>\(?\d{3}\)?-?\s?\d{3}-\d{4})?\t
      (?P<job>[\w\s]+, [\w\s.]+)\t?
      (?P<twitter>@[-\w\d]+)?$
      ''',re.X|re.M)
    for match in line.finditer(data):
      name.append(match.group('name'))
      email.append(match.group('email'))
      phone.append(match.group('phone'))
      job.append(match.group('job'))
      twitter.append(match.group('twitter'))
    t=len(name)+1
    a=[]
    b=0
    c=-1
    for item in range(t):
      for items in range(6):
        if c==4:
          b+=1
          c=-1
          continue
        else:
          c+=1
          d=b,c
          a.append(d)
          del d
    return name,email,phone,job,twitter,a

  def name_length(name):
    """return the length of the name

    >>> t=['vikas','pratibha','hii']
    >>> f=name_length(t)
    >>> f==8
    True

    """
    check=[]
    for item in name:
      if item==None:
        continue
      else:        
        check.append(len(item))
    else:
      check.sort()
      leng=len(check)
      sort1=check[leng-1]
      del check
    return sort1

  def map(hello,a,twitter,twi,catch,name,catch1,ema,email,pho,phone,catch2,jo,job):
    under={'name':(0,0),'email':(0,1),'phone':(0,2),'job':(0,3),'twitter':(0,4)}
    for idx,item in enumerate(a):
      if idx in [4,9,14,19,24,29,34,39,44,49,54,59,64]:
        if item in under.values():
          for j,k in under.items():
            if k==item:
              done=j
            hello1=twi-len(done)
          print('|{}{}|'.format(done.upper(),'_'*hello1))
        else:
          try:
              first=twitter[0]
          except TypeError:
            pass
          else:
            if twitter[0]==None:
              hello1=twi-1
              print('|{}{}|'.format('-','_'*hello1))
              del twitter[0]
            else:
              hello1=twi-len(first)
              print('|{}{}|'.format(first,'_'*hello1))
              twitter.remove(first)
      else:
        if item in under.values():
          for j,k in under.items():
            if k==item:
              done=j
          if done=='name':
            hello1=hello-len(done)
          elif done=='email':
            hello1=ema-len(done)
          elif done=='phone':
            hello1=pho-len(done)
          else:
            hello1=jo-len(done)
          print('|{}{}'.format(done.upper(),'_'*hello1),end=' ')
        elif idx in catch:
          first=name[0]
          hello1=hello-len(first)
          print('|{}{}'.format(first,'_'*hello1),end=' ')
          name.remove(first)
        elif idx in catch1:
          first=email[0]
          hello1=ema-len(first)
          print('|{}{}'.format(first,'_'*hello1),end=' ')
          del email[0]
        elif idx in catch2:
          if phone[0]==None:
            hello1=pho-1
            print('|{}{}'.format('-','_'*hello1),end=' ')
            del phone[0]
          else:
            first=phone[0]
            hello1=pho-len(first)
            print('|{}{}'.format(first,'_'*hello1),end=' ')
            del phone[0]
        else:
          if job[0]==None:
            hello1=jo-1
            print('|{}{}'.format('-','_'*hello1),end=' ')
            del job[0]
          else:
            first=job[0]
            first=first.replace('\t','')
            hello1=jo-len(first)
            print('|{}{}'.format(first,'_'*hello1),end=' ')
            del job[0]
  while True:
    name,email,phone,job,twitter,a=start()
    hello=name_length(name)
    twi=name_length(twitter)
    ema=name_length(email)
    pho=name_length(phone)
    jo=name_length(job)
    catch=len(a)
    catch=list(range(catch+1))
    del catch[0]
    catch=catch[4::5]
    catch1=len(a)
    catch1=list(range(catch1+1))
    del catch1[0]
    catch1=catch1[5::5]
    catch2=len(a)
    catch2=list(range(catch2+1))
    del catch2[0]
    catch2=catch2[6::5]
    map(hello,a,twitter,twi,catch,name,catch1,ema,email,pho,phone,catch2,jo,job)
    os.system('cls')    

[MOD: added ```python formatting -cf]

Chris Freeman
Chris Freeman
68,469 Points

Can you please post sample data to use for names.txt?

here it is:

Love , Kenneth  [email protected]   (555) 555-5555  Teacher, Treehouse  @kennethlove
McFarland, Dave [email protected]  (555) 555-5554  Teacher, Treehouse
Arthur, King    [email protected]       King, Camelot
Österberg, Sven-Erik   [email protected]       Governor, Norrbotten    @sverik
, Tim   [email protected]        Enchanter, Killer Rabbit Cave
Carson, Ryan    [email protected]  (555) 555-5543  CEO, Treehouse  @ryancarson
Doctor, The [email protected]       Time Lord, Gallifrey
Exampleson, Example [email protected]  555-555-5552    Example, Example Co.    @example
Obama, Barack   [email protected] 555 555-5551    President, United States of America @potus44
Chalkley, Andrew    [email protected]    (555) 555-5553  Teacher, Treehouse  @chalkers
Vader, Darth    [email protected]  (555) 555-4444  Sith Lord, Galactic Empire  @darthvader
Fernández de la Vega Sanz, María Teresa   [email protected]     First Deputy Prime Minister, Spanish Govt.

1 Answer

Chris Freeman
Chris Freeman
68,469 Points

I am confused by the code structure. The while loop is inside the definition of the class. Executable code not inside a method is unusual in a class definition. The while has a True argument, which means it will never complete, so it's stuck in an infinite loop. The test never gets to run because the class never finishes being defined due to the infinite loop.

I make "while True" and then all method inside it intentionally because i want to make a program which will change the information when it is changed in the originally file making it a real-time viewer.So it means i cannot test this program because it will never stop.