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

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter6: Modularity Using Functions
Section6.2: Returning A Single Value
Problem 13E
icon
Related questions
Question

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.

Expert Solution
Step 1

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

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Knowledge Booster
Datatypes
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
Programming with Microsoft Visual Basic 2017
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:
9781337102124
Author:
Diane Zak
Publisher:
Cengage Learning
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,