In: Computer Science
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)
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...