Search code examples
pythonpseudocodepython-idle

PseudoCode for Python: finding an item in a list


I'm working on pseudocode for a program that will allow a single listitem to be found in a listofitems. Pseudocode is still a bit of a foreign concept to me, and I could be going about this 110% wrong... so this is the pseudocode...

listofitems= [1, 2, 'a']

define function findItem(object1, object2, object3):
    listItem = raw_input("Find this item in the list: ")
    for each item in findItem:
        if listItem = object1 in findItem or listItem = object2 in findItem or listItem = object3 in findItem:
            print True
        else:
            print False

findItem(listofitems):

how left from right am I???


Solution

  • 17 years ago I called Python 'executable pseudocode' because it at least partly replaces the need for unexecutable and therefore untestable pseudocode. Better, in a case like yours, to write test data (as you did), test code (which you did not), and real code that can be tested. While I recommend beginning with Python 3 if possible, I started the below with 3 lines to make it run the same on Python 2.

    import sys
    if sys.version_info.major < 3:
        input = rawinput
    
    mylist = ['1', '2', 'a']
    myset = {'3.14', 'b'}
    
    def find(target, iterable):
        for item in iterable:
            if item == target:
                return True
        return False
    
    assert find('2', mylist) is True
    assert find('2', myset) is False
    
    # print(find(input("Item to find")))