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

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2

    Help on global variables


    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



    I dont know where to start with the find item function, any tips?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    #See if you can do this much:

    Code:
    def find(item):
        #increment searchCount
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2
    Originally Posted by b49P23TIvg
    #See if you can do this much:

    Code:
    def find(item):
        #increment searchCount
    I was able to do the following but im not too sure if its correct:

    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():


    I need help with the def result()
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    Now, when you post python code the indentation is critical. Please use code tags (see my signature) instead of bold tags.

    You're not close, but at least you've tried. foundCount and searchCount are assigned values within a function. Therefor, since the assignments are gobal, they need to be declared as global scope. Names in python are case sensitive. foundCount is not the same variable as FOUNDCOUNT is not the same as foundcount.
    Code:
    foundCount = 0
    searchCount = 0
    names = [ "Mary", "Liz", "Miles", "Bob", "Fred"]
    numbers = [ 4, 17, 19]
    
    def find(item):  # find() takes a single parameter
        global foundCount, searchCount
        searchCount += 1   # always increments searchCount
        if item in names:  # looks up in the global list of names
            print('Found in Names') # and prints "Found in Names"
            foundCount += 1  # if it finds the item, it increments the foundCount
            #'Lambert Electronics, LLC.  NY, USA'
        elif item in numbers: # then it looks in the list of numbers.
            print('Found in Numbers') # and prints "Found in Numbers"
            foundCount += 1  # if it finds the item, it increments the foundCount
        else:
            print('Not found') # displays "Not found" if it can't find the item
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    55
    Rep Power
    2
    Originally Posted by b49P23TIvg
    Now, when you post python code the indentation is critical. Please use code tags (see my signature) instead of bold tags.

    You're not close, but at least you've tried. foundCount and searchCount are assigned values within a function. Therefor, since the assignments are gobal, they need to be declared as global scope. Names in python are case sensitive. foundCount is not the same variable as FOUNDCOUNT is not the same as foundcount.
    Code:
    foundCount = 0
    searchCount = 0
    names = [ "Mary", "Liz", "Miles", "Bob", "Fred"]
    numbers = [ 4, 17, 19]
    
    def find(item):  # find() takes a single parameter
        global foundCount, searchCount
        searchCount += 1   # always increments searchCount
        if item in names:  # looks up in the global list of names
            print('Found in Names') # and prints "Found in Names"
            foundCount += 1  # if it finds the item, it increments the foundCount
            #'Lambert Electronics, LLC.  NY, USA'
        elif item in numbers: # then it looks in the list of numbers.
            print('Found in Numbers') # and prints "Found in Numbers"
            foundCount += 1  # if it finds the item, it increments the foundCount
        else:
            print('Not found') # displays "Not found" if it can't find the item
    Thank you so much for the help, i finished it off with:

    def results():
    printNow( "%32s \n%s %d \n%s %d \n%s %d " % ("***** Search Results *****","Total searches:", searchCount,"Total matches:", foundCount,"Total not found:", searchCount-foundCount))

IMN logo majestic logo threadwatch logo seochat tools logo