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

Implement the program as follows:

  1. define the function is_string_in_list() that accepts a list of string and a string value
  2. initialize is_present to False
  3. Use 'in' operator to check if the list contains str_val. 'in' operator returns True is value is present in list
  4. Update is_present to True if str_val is present in list
  5. return is_present
  6. create a list, list1
  7. call is_string_in_list() to check if the list contains a particular string value

Program:

Note: Please double-check the indentation using the screenshot provided as a reference



def is_string_in_list(list_of_string, str_val):     # define the function is_string_in_list() that accepts a list of string and a string value
    is_present = False                              # initialize is_present t0 False
    if str_val in list_of_string:                   # Use 'in' operator to check if the list contains str_val 
        is_present = True                           # Update is_present to True if str_val is present in list
    return is_present                               # return is_present
    
list1 = ['eggs', 'milk', 'bananas']                 # create a list, list1
print('list1', list1)
print(is_string_in_list(list1, 'pumpkin'))          # call is_string_in_list() to check if the list contains a particular string value

list2 = ['eggs', 'milk', 'bananas', 'pumpkin']
print('list2', list2)
print(is_string_in_list(list2, 'pumpkin'))

Screenshot:

Output:

Please don't forget to give a Thumbs Up.


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
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....
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...
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
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