0

As an exercise I have been tasked with writing a function that will check if a given container contains a searched value, returning True if it does and False if it doesn't. I have created the following function:

setContainer = {i for i in range(1000)}
listContainer = [i for i in range(1000)]

def does_it_contain(container, value):
    if value in container:
        return True
    if value not in container:
        return False

which works fine when I invoke the function explicitly.

However I am trying to add some code to let the user search for a value in the container of his choice like:

checkValue = input("Search for: ")
checkContainer = input("Seach in container named: ")

print(does_it_contain(checkContainer, checkValue))

But this seems to always yield False, and I can't figure out why. Where have I gone wrong?

Sapna K
  • 11
  • 2
  • hi, and welcome to StackOverflow. When you say use `checkContainer` as your container, it is simply a string, not a container with values. – rajah9 Nov 25 '20 at 16:23

4 Answers4

2

Because checkContainer is a string, passing a string to does_it_contain function will let it to check whether the value is in the particular checkContainer string instead of the container object.

If you want to find the correct container by its name, you could create a dictionary.

# Using dictionary

dict = {
    "a": [...],
    "b": {...}
}

def func(name, val):
    print(val in dict[name])
PIG208
  • 2,060
  • 2
  • 10
  • 25
1

The problem is that the user inputs a string which can be identical to the name of your container's name, but it won't be the container object you defined in your code.

Instead you should create a mapping, e.g. a dictionary, in which you store the reference of your containers, and you can pick the right one based on the user's input:

setContainer = {i for i in range(1000)}
listContainer = [i for i in range(1000)]

container_switcher = {
    "setContainer": setContainer,
    "listContainer": listContainer,
}


def does_it_contain(container, value):
    if value in container:
        return True
    if value not in container:
        return False


checkValue = input("Search for: ")
checkContainer = input("Seach in container named: ")

container = container_switcher.get(checkContainer, None)
if container is None:
    print("Invalid container name: " + checkContainer)
else:
    print(does_it_contain(container, int(checkValue)))

Note that you have to convert the checkValue to an integer when passing to the does_it_contain function, otherwise you will pass it as a string and it will also always return False.

Szabolcs
  • 3,990
  • 18
  • 38
0

There are two problems in your code. You are searching for a string in a set of integers, and that you are just getting a string containing either setContainer or listContainer, and not the variable values themselves.

To fix the first issue, use str(i) for i in range(1000) instead of i for i in range(1000).

For the second issue, change if value in container: to if value in containers[container]: and to the same for the second if statement. Then, change

setContainer = {i for i in range(1000)}
listContainer = [i for i in range(1000)]

to

containers = {
  'setContainer': {str(i) for i in range(1000)},
  'listContainer': [str(i) for i in range(1000)]
}

so that you can access the values using a string like so: containers[container].

containers = {
  'setContainer': {str(i) for i in range(1000)},
  'listContainer': [str(i) for i in range(1000)]
}


def does_it_contain(container, value):
    if value in containers[container]:
        return True
    if value not in containers[container]:
        return False


checkValue = input("Search for: ")
checkContainer = input("Seach in container named: ")

print(does_it_contain(checkContainer, checkValue))

This will throw an error if the container doesn't exist, so you'll have to wrap print(does_it_contain(checkContainer, checkValue)) in a try...except block to check if the user entered an invalid container name.

shreyasm-dev
  • 2,711
  • 5
  • 16
  • 34
0

As also noted in other answers, your code has two issues: looking up a string in a list of int's and passing a string with the intent to pass a variable name. Workarounds for both can be found in these answers.

That being said, if you really wanted to directly get the variable name using an input field, there are different possibilities for that as well. See Convert a string to preexisting variable names for details and also for hints on how not to do it (i.e. don't use eval()!)

One way would be to use the globals() function in python that gives you a dictionary of all defined variables:

setContainer = {str(i) for i in range(1000)}
listContainer = [str(i) for i in range(1000)]

def does_it_contain(container, value):
    if value in container:
        return True
    if value not in container:
        return False
        
checkValue = input("Search for: ")
checkContainerVarName = input("Seach in container named: ")

print(does_it_contain(globals()[checkContainerVarName], checkValue))

will do what you originally intended.

buddemat
  • 4,552
  • 14
  • 29
  • 49