Some of the earliest computer games developed were Interactive Fiction games, in which the user’s environment is described in text, and the user makes choices using text commands. In this problem and the next one, we’ll be developing a very simple text-based adventure game. Every choice in this game will have exactly three options, so we can write a function that works for any of them. Write a function selection(text, optionA, optionB, optionC), that takes in four string values. text is a string representing a prompt in a text adventure game, and optionA, optionB, and optionC are strings representing the three possible options
Some of the earliest computer games developed were Interactive Fiction games, in which the user’s environment is described in text, and the user makes choices using text commands. In this problem and the next one, we’ll be developing a very simple text-based adventure game. Every choice in this game will have exactly three options, so we can write a function that works for any of them.
Write a function selection(text, optionA, optionB, optionC), that takes in four string values. text is a string representing a prompt in a text adventure game, and optionA, optionB, and optionC are strings representing the three possible options.
The function should print out the text, and then print out the options (label them with A., B., and C.). Next, the input() function should be used to prompt the user to choose A, B, or C. Then the function should return (not print) the one character string that represents the user’s choice: 'A', 'B', or 'C'. If the user does not choose one of those options, then the function must print "Invalid option, defaulting to A" and then return 'A'.
See the examples on the next page for an idea of how to format the printing and input functions: you don’t have to match it exactly but it should be roughly the same (you do have to match the return exactly though: returning lowercase 'a', 'b', or 'c' will cause you to fail test cases).
Hint:
-
When calling functions that return something like input, you generally want to assign the result to a variable so that you can access it later: var = input(...)
-
You do need to make sure your variable names describe the value they represent, so don’t actually use var as a variable name in this problem.
Python Function: It is code defined in the block and it executed when it is called
Python function
def function_name():
.........
return
Python code:
#python function definition
def selection(text, optionA, optionB, optionC):
print(text)#printting text
print("A.",optionA)#printing optionA
print("B.",optionA)#printing option B
print("C.",optionA)# printing option C
#choose is used to stre choosed option
choose=input("Choose A, B, or C: ")
if choose=='A':
return 'A' #if choosen option A
elif choose=='B':
return 'B' #if choosen option B
elif choose=='C':
return 'C' #if choosen option C
else:#if no condition is matched
print("Invalid option, defaulting to A");
return 'A'
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images