Define a function with following specs.
Takes two argments: x: a scaler l: a list The function will check whether x exists in the list, and print out the results if exists: yes if not: no For example:
func1(x = 12, l = [1,45,5,3]) Will produce
12 is no
def inList(x=12,l=[1,45,5,3]):
for x in fuc1:
if x in 1:
print("value is in the list")
else:
print("value is not in the list")
CodePudding user response:
this can be a simple if statement
If x in i:
print("It is here")
else:
print("It is not here")
CodePudding user response:
just as simple:
def is_in_list(x, l):
if x in l:
print('in list')
return
else:
print('not in list')
if you can't use in
or you are trying to do it using loops then you can do so:
def is_in_list(x, l):
for item in l:
if(item == x):
print('in list')
return
print('not in list')
CodePudding user response:
You can just do
["No", "Yes"][x in l]
But, as @chepner commented, it seems you are expected to write your own implementation of in
, so you should avoid using it in the first place.
What about looking at each item of l
and testing whether is the same or not as your target. Do you know how to test if tow items have the same value?
(BTW, avoid using l
as name. It looks like a number 1 in many editors)
CodePudding user response:
You have missed the variable names up. Also, there is no need for a for loop in this case.
def inList(x=12,l=[1,45,5,3]):
if x in l:
print("value is in the list")
else:
print("value is not in the list")
CodePudding user response:
I hope this will help you:
MyList = [1,45,5,3]
x=12
def my_function(MyList , x):
# Print list
print("Our List: ", MyList )
# Check if 'b' exists in the list or not
if x in MyList:
print(" value is in the list")
else:
print(" value is not in the list")
my_function(MyList, x)