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

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2

    Python programming help


    Using four global variables

    foundCount = 0
    searchCount = 0
    names = [ "Mary", "Liz", "Miles", "Bob", "Fred"]
    numbers = [ 4, 17, 19]

    def find(item):
    ....

    def result():
    ....

    Write code for the body of the two functions:

    find()
    1.takes a single parameter which it looks up in the global list of names, and if it's not found there, then it looks in the list of numbers.
    2.always increments searchCount, whether the item was found or not
    3.if it finds the item, it increments the foundCount and prints "Found in Names" or "Found in Numbers" as appropriate.
    4.displays "Not found" if it can't find the item
    results()
    this has no parameters. It prints a summary of the search counts:
    e.g. "Total Searches: 6, Found items: 4, Not found: 2"

    Sample run
    ======= Loading Program =======
    >>> find("mary")
    Not found
    >>> find("Mary")
    Found in names
    >>> find(0)
    Not found
    >>> find(19)
    Found in numbers
    >>> results()
    ***** Search Results *****
    Total searches: 4
    Total matches : 2
    Total not found: 2



    After hours and hours of trying, i was able to do the following code:

    foundCount = 0
    searchCount = 0
    names = [ "Mary", "Liz", "Miles", "Bob", "Fred"]
    numbers = [ 4, 17, 19]

    def find(item):
    global foundcount
    if item == "Mary":
    printNow("Found in names")
    elif item == "Liz":
    printNow("Found in names")
    elif item == "Miles":
    printNow("Found in names")
    elif item == "Bob":
    printNow("Found in names")
    elif item == "Fred":
    printNow("Found in names")
    elif item == "4":
    printNow("Found in numbers")
    elif item == "17":
    printNow("Found in numbers")
    elif item == "19":
    printNow("Found in numbers")
    else: printNow("Not Found")

    def result():


    Is this right? i need help with the def result() as i dont know how to apply the global variable to show results
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0
    This should do what you want:
    Code:
    foundCount = 0
    searchCount = 0
    names = [ "Mary", "Liz", "Miles", "Bob", "Fred"]
    numbers = [ 4, 17, 19]
    
    def find(item):
        global foundCount
        global searchCount
        
        found = False
        searchCount += 1
        
        for e in names:
            if item == e :
                foundCount += 1
                print("Found", item ,"in global parameter names")
                found = True
                break
            
        for e in numbers:
            if item == e :
                foundCount += 1
                print("Found", item ,"in global parameter numbers")
                found = True
                break
    
        if not(found):
            print("Item not found")
            
                
    def result():
        print("{0:<20}{1}".format("Total Searches:",searchCount))
        print("{0:<20}{1}".format("Found Items:",foundCount))
        print("{0:<20}{1} \n".format("Not Found",searchCount-foundCount))
    
    
    if __name__ == "__main__":
        find(4)
        result()
    
        find('Mary')
        result()
        
        find(5)
        result()
    
        find('Maryy')
        result()

IMN logo majestic logo threadwatch logo seochat tools logo