#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    85
    Rep Power
    1

    [Python 2 Name Script] Name Delete not works, help needed!


    Hey Python Programmers...

    I need help with my script, thad must be an simple possibillity to typ names (the names in
    my tupple thad you can see above in my script), add names and delete names...

    This is my script, in this case in python 2.7 (i use python 2.7 only on an old computer,
    in the glazenpui in Vlissingen... normally, i use python 3):
    Code:
    Run = True
    
    HOBBYS = {'Jamie' : 'Natuurkunde',
              'Ellister' : 'Darten',
              'Sander' : 'Lezen',
              'Coert' : 'Vrijwilliger',
              'John' : 'Radio',
              'Rob' : 'Speelgoed',
              'Frank' : 'Televisie'}
    
    
    while Run:
        try:
            NAMES = str(raw_input('Typ een Naam: '))
    
            if NAMES in HOBBYS:
                print('{0} houd van {1}'.format(NAMES, HOBBYS[NAMES]))
            elif NAMES == 'Add':
                ANAME = str(raw_input('Name: '))
                AHOBBY = str(raw_input('Hobby: '))
                HOBBYS.update({ANAME : AHOBBY})
            elif NAMES == 'Delete':
                DNAME = str(raw_input('Name: '))
                DHOBBY = str(raw_input('Hobby: '))
                HOBBYS.delete({DNAME : DHOBBY})
            elif NAMES == 'Stop':
                Run = False
    
        except:
            print('Sessie Mislukt, Naam niet Ondersteunt!')

    If i try to delete an name and an hobby, it don't works... my script give than my called
    self made error message (in the except), but the means is thad you can delete an
    name, with the giving hobby..., but python doesn't...

    Normally i use python 3, but this script may i written in the glazenpui on an older windows
    system (called Windows XP)... just its written in python 2.7....

    Can anyone help me to correct my code, this is an simple test, to test my python talent...

    Thanks for help, Jamie.
  2. #2
  3. Lord of the Dance
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Posts
    4,170
    Rep Power
    2011
    First, I will suggest you to learn more about exception handling and how to print the error message in the exception part.
    Second, where did you find that delete statement? Lookup on how to delete from a dict(onary)
  4. #3
  5. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,971
    Rep Power
    510
    dictionaries don't have a delete method.
    Code:
    >>> 'update' in dir({})
    True
    >>> 'delete' in dir({})
    False
    >>>>>> '__delitem__' in dir({})
    True
    >>>
    Dictionaries support the del statement. We have
    Code:
    Run = True
    
    HOBBYS = {'Jamie' : 'Natuurkunde',
              'Ellister' : 'Darten',
              'Sander' : 'Lezen',
              'Coert' : 'Vrijwilliger',
              'John' : 'Radio',
              'Rob' : 'Speelgoed',
              'Frank' : 'Televisie'}
    
    
    while Run:
        try:
            NAMES = str(raw_input('Typ een Naam: '))
    
            if NAMES in HOBBYS:
                print('{0} houd van {1}'.format(NAMES, HOBBYS[NAMES]))
            elif NAMES == 'Add':
                ANAME = str(raw_input('Name: '))
                AHOBBY = str(raw_input('Hobby: '))
                HOBBYS.update({ANAME : AHOBBY})
            elif NAMES == 'Delete':
                DNAME = str(raw_input('Name: '))
                # DHOBBY = str(raw_input('Hobby: '))
                del HOBBYS[DNAME]
            elif NAMES == 'Stop':
                Run = False
    
        except:
            print('Sessie Mislukt, Naam niet Ondersteunt!')

    Let's support multiple hobbies:
    Code:
    '''
        This version supports multiple hobbies.
    '''
    
    import pprint
    
    Run = True
    
    HOBBYS = {'Jamie' : ['Natuurkunde'],
              'Ellister' : ['Darten'],
              'Sander' : ['Lezen'],
              'Coert' : ['Vrijwilliger'],
              'John' : ['Radio'],
              'Rob' : ['Speelgoed'],
              'Frank' : ['Televisie']}
    
    pprint.pprint(sorted(HOBBYS.keys()))
    
    while Run:
        try:
            NAMES = str(raw_input('Typ een Naam: '))
    
            if NAMES in HOBBYS:
                print('{0} houd van {1}'.format(NAMES, HOBBYS[NAMES]))
            elif NAMES == 'Add':
                ANAME = str(raw_input('Name: '))
                AHOBBY = str(raw_input('Hobby: '))
                if ANAME in HOBBYS:
                    HOBBYS[ANAME] += [AHOBBY]
                else:
                    HOBBYS.update({ANAME : AHOBBY})
            elif NAMES == 'Delete':
                DNAME = str(raw_input('Name: '))
                if DNAME in HOBBYS:
                    if len(HOBBYS[DNAME]) < 2:
                        del HOBBYS[DNAME]
                    else:
                        DHOBBY = str(raw_input('Hobby: '))
                        L = HOBBYS[DNAME]
                        if DHOBBY in L:
                            del L[L.index(DHOBBY)]
            elif NAMES == 'Stop':
                Run = False
    
        except:
            print('Sessie Mislukt, Naam niet Ondersteunt!')

    Let's include a database. This program uses hobby.pkl file in current directory.
    Code:
    '''
        This version supports multiple hobbies.
        This program uses hobby.pkl file in current directory as a pickled database
    '''
    
    import textwrap
    import pprint
    import pickle
    
    db = 'hobby.pkl'
    
    Run = True
    
    try:
        with open(db, 'rb') as inf:
            HOBBYS = pickle.load(inf)
    except:
        HOBBYS = {'Jamie' : ['Natuurkunde'],
                  'Ellister' : ['Darten'],
                  'Sander' : ['Lezen'],
                  'Coert' : ['Vrijwilliger'],
                  'John' : ['Radio'],
                  'Rob' : ['Speelgoed'],
                  'Frank' : ['Televisie']}
    
    print(textwrap.wrap('names: ' + ' '.join(sorted(HOBBYS.keys())))[0])
    print('commands: Add Delete Save Stop')
    
    while Run:
        try:
            NAMES = str(raw_input('Typ een Naam: '))
    
            if NAMES in HOBBYS:
                print('{0} houd van {1}'.format(NAMES, HOBBYS[NAMES]))
            elif NAMES == 'Add':
                ANAME = str(raw_input('Name: '))
                AHOBBY = str(raw_input('Hobby: '))
                if ANAME in HOBBYS:
                    HOBBYS[ANAME] += [AHOBBY]
                else:
                    HOBBYS.update({ANAME : AHOBBY})
            elif NAMES == 'Delete':
                DNAME = str(raw_input('Name: '))
                if DNAME in HOBBYS:
                    if len(HOBBYS[DNAME]) < 2:
                        del HOBBYS[DNAME]
                    else:
                        DHOBBY = str(raw_input('Hobby: '))
                        L = HOBBYS[DNAME]
                        if DHOBBY in L:
                            del L[L.index(DHOBBY)]
            elif NAMES == 'Save':
                with open(db, 'wb') as ouf:
                    pickle.dump(HOBBYS, ouf)
            elif NAMES == 'Stop':
                Run = False
    
        except:
            print('Sessie Mislukt, Naam niet Ondersteunt!')
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo