Question

In: Computer Science

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 the user selected.

Important Note:

DO NOT USE INPUT AND PRINT INSIDE THE FUNCTION AT ALL
MAKE THEM OUTSIDE THE FUNCTION PLEASE!

Example:

myList = myFunction(["yes","no","maybe"])

select one of the following please:

yes

no

maybe

# user input example

> could be

select one of the following please:

> perhaps

select one of the following please:

> yes

print(myList)

Solutions

Expert Solution

Please find the Python code for the following:

Code:

#Defined a function that takes a list of string as arguments.
def myFunction(stringList):
  
#When the function is called
#it should ask the user to make a selection from the options listed in the given list.
print("select one of the following please:")
for i in stringList:
print(i)
  
#We are creating a infinite while loop, till the user enters correct choice
while True:
  
#Input the string by placing " >" in front of user input.
inputString=input(">")
  
#If the inputString is not in the list
#Print the message and continue to while loop
if inputString not in stringList:
print("select one of the following please:")
  
#In the else part(user response is in the list)
#So, return the word the user selected.
else:
return inputString


myList = myFunction(["yes","no","maybe"])
print(myList)

Please check the compiled program and its output for your reference:

Output:

(I believe that I made the code simple and understandable. If you still have any query, Feel free to drop me a comment)

Hope this Helps!!!
Please upvote as well, If you got the answer?
If not please comment, I will Help you with that...


Related Solutions

PYTHON Basic Feature[5pts] Write a function which takes two arguments—a string (str1) and a position index...
PYTHON Basic Feature[5pts] Write a function which takes two arguments—a string (str1) and a position index (n). The function will: 1.Remove the n-th characterof str1 (the initial character is the 0-th character) 2.Swap the order of the substring in front of the removed character and the substring afterwards, and concatenate them as a new string 3.Return the converted (str2) back to the function caller For example, myfunc(“abc1xyz”, 3) returns “xyzabc”. Additional Features [6 pts] Expand the function by adding more...
C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
Python please Write a function that takes a string as an argument checks whether it is...
Python please Write a function that takes a string as an argument checks whether it is a palindrome. A palindrome is a word that is the same spelt forwards or backwards. Use similar naming style e.g. name_pal. E.g. If we call the function as abc_pal(‘jason’) we should get FALSE and if we call it a abc_pal(‘pop’) we should get TRUE. Hint: define your function as abc_pal(str). This indicates that string will be passed. Next create two empty lists L1=[] and...
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]...
Use Python Write a function that takes a mobile phone number as a string and returns...
Use Python Write a function that takes a mobile phone number as a string and returns a Boolean value to indicate if it is a valid number or not according to the following rules of a provider: * all numbers must be 9 or 10 digits in length; * all numbers must contain at least 4 different digits; * the sum of all the digits must be equal to the last two digits of the number. For example '045502226' is...
Write a function which takes two words as string arguments and checks whether they are anagrams.
Write a function which takes two words as string arguments and checks whether they are anagrams.
Write functions in Python IDLE that do the following: i) A function that takes 2 arguments...
Write functions in Python IDLE that do the following: i) A function that takes 2 arguments and adds them. The result returned is the sum of the parameters. ii) A function that takes 2 arguments and returns the difference, iii) A function that calls both functions in i) and ii) and prints the product of the values returned by both.
(In python) Write a function calc_pizza_charge that takes four integer arguments, one for the size (1...
(In python) Write a function calc_pizza_charge that takes four integer arguments, one for the size (1 small, 2 medium, 3 large), one for the number of meat toppings, one for the number of other toppings and another for the quantity of pizzas ordered. It should calculate and return the total due for that pizza order based on the following information: Small pizza base price: $6.50 Medium pizza base price: $9.50 Large pizza base price: $11.50 The base pizza price includes...
(Python) a) Using the the code below write a function that takes the list xs as...
(Python) a) Using the the code below write a function that takes the list xs as input, divides it into nss = ns/nrs chunks (where nrs is an integer input parameter), computes the mean and standard deviation s (square root of the variance) of the numbers in each chunk and saves them in two lists of length nss and return these upon finishing. Hint: list slicing capabilities can be useful in implementing this function. from random import random as rnd...
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT