215k views
1 vote
Selector

Next we will write a function that will help us select an output for the chatbot, based on the input it got.
The overall goal of this function is to take a list of words that we got as input, a list of words to check for if they appear in the input outputs to return if something from the list to check is in the input list.
Define a function, called selector.
This function should have the following inputs, outputs and internal procedures:
Input(s)
input_list - list of string
check_list - list of string
return_list - list of string
Output(s):
output - string, or None
Procedure(s):
Initialize output to None
Loop through each element in input_list
Use a conditional to check if the current element is in check_list
If it is, assign output as the returned value of calling the random.choice function on return_list
Also, break out of the loop
At the end of the function, return output Note that if we don't find any words from input_list that are in check_list, output will be returned as None.

User Mujaffars
by
5.6k points

1 Answer

2 votes

Answer:

See explaination

Step-by-step explanation:

# Import required the module.

import random

# Define a function selector() which accepts the

# input_list, check_list, and return_list and

# returns a string named output.

def selector(input_list,check_list,return_list):

# Assign None to string variable output.

output=None

# Use for loop to traverse through input_list.

for i in range(len(input_list)):

# Check if element of an input_list is present

# in check_list.

if input_list[i] in check_list:

# Assign a random value from return_list

# to output.

output=random.choice(return_list)

# break out of the loop if input_list

# element is present in check_list.

break

#

return output

# Use assert statement to test a condition.

# If the condition is true, then program continues to

# execute, otherwise raises an AssertionError.

assert callable(selector)

assert selector(['is','in','of'],['of'], ['Yes']) == 'Yes'

assert selector(['is','in'],['of'], ['Yes','No']) == None

# Display the output.

print(selector(['is','in','of'],['of'], ['Yes']))

print(selector(['is','in'],['of'], ['Yes','No']))

User Nick Prozee
by
6.2k points