Question

In: Computer Science

Write a Python function that takes two parameters: the first a list of strings and the...

Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.

Solutions

Expert Solution

I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments (read them for better understanding).

Images of the Code:
Note: If the below code is missing indentation please refer code Images

Typed Code:

# A function called contains_string with input parameters
# List_strings is a List of strings
# string is a word(string)
def contains_string(List_strings ,string):
# traversing through all the elements of the List
for x in List_strings:
# if x is same as string
if(x==string):
# returning True
return True
# if string is not in List_strings
# then returning False
return False
  
# A List of strings
List_strings = ["eggs", "milk", "bananas"]
# A variable string with value pumpkin
string = "pumpkin"
# calling the function contains_string
# printing the returned value
print("List of strings contains ",string,": ",contains_string(List_strings,string))
#code ended here

Output:


If You Have Any Doubts. Please Ask Using Comments.

Have A Great Day!


Related Solutions

Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Design a function in python that takes a list of strings as an argument and determines...
Design a function in python that takes a list of strings as an argument and determines whether the strings in the list are getting decreasingly shorter from the front to the back of the list
This is an intro to python question. #Write a function called search_for_string() that takes two #parameters,...
This is an intro to python question. #Write a function called search_for_string() that takes two #parameters, a list of strings, and a string. This function #should return a list of all the indices at which the #string is found within the list. # #You may assume that you do not need to search inside the #items in the list; for examples: # # search_for_string(["bob", "burgers", "tina", "bob"], "bob") # -> [0,3] # search_for_string(["bob", "burgers", "tina", "bob"], "bae") # -> []...
using python 1. #Write a function called multiply_file_by_index. This function #should take two parameters, both strings....
using python 1. #Write a function called multiply_file_by_index. This function #should take two parameters, both strings. The first string is #the filename of a file to which to write (output_file), and #the second string is the filename of a file from which to read #(input_file). # #In the input file, there will be an integer on every line. #To the output file, you should write the integer from the #original file multiplied by the line number on which it #appeared....
1. Implement the function calculate_score that consumes three parameters, two strings and a list. The strings...
1. Implement the function calculate_score that consumes three parameters, two strings and a list. The strings will each be ONE character and will represent a nucleotide. The list will be a nested int list representing a 4x4 score matrix. This function will return the value (int) from the nested int list at the location of the two referenced nucleotides. a. An example call to calculate_score would be calculate_score(“A”, “T”, score_matrix). If we look at the alignment score table in the...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the user to enter a password until the entered password has 8-15 characters, including at least one digit. Tell the user whenever a password fails one or both of these tests.
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.   Hint: Use a whlie loop and list methods lst = [1,3,5,7]...
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
Write a Python function that takes as input parameters base_cost (a float) and customer_type and prints...
Write a Python function that takes as input parameters base_cost (a float) and customer_type and prints a message with information about the total amount owed and how much the tip was. As a reminder, the tip amounts are 10%, 15% and 20% for stingy, regular, and generous customers. And the tax amount should be 7%. The total amount is calculated as the sum of two amounts: check_amount = base_cost*1.07 tip_amount = tip_percentage*check_amount To receive full credit, you must use string...
Python question Define a function called selection_order(items, interval) which takes two parameters: items is a list...
Python question Define a function called selection_order(items, interval) which takes two parameters: items is a list of elements and interval is an integer larger than 0. Imagine that the elements in items were arranged in a circle. Including the first element, count off the elements in items up to the interval position and then remove that element from the circle. From that position, begin counting the positions of the elements again and remove the element that has the next interval...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT