user2205841
user2205841

Reputation: 1

Need tips in Global Variables Assignment in Python for assignment

Need help with this assignment:

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

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.

always increments searchCount, whether the item was found or not

if it finds the item, it increments the foundCount and prints "Found in Names" or "Found in Numbers" as appropriate.

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**

What i have done so far is:

**

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 def result() function so i could finish this assignment and study for my maths exam in three days.. Need help in doing this assignment as it is due in tomorrow :(....

Upvotes: 0

Views: 61

Answers (1)

Blender
Blender

Reputation: 298432

Use the in operator:

def find(item):
    global foundcount

    if item in names:
        foundCount += 1
        printNow("Found in names")
    elif item in numbers:
        foundCount += 1
        printNow("Found in numbers")
    else:
        printNow("Not Found")

As for result, you can use the print function:

print('Found Items:', foundCount)

Upvotes: 1

Related Questions