Question: THE PREVIOUS FUNCTION def choice(text, optionA, optionB, optionC): #printing the question print(text) #printing the options print(A., optionA) print(B., optionB) print(C., optionC) #reading the option from

THE PREVIOUS FUNCTION

def choice(text, optionA, optionB, optionC): #printing the question print(text) #printing the options print("A.", optionA) print("B.", optionB) print("C.", optionC) #reading the option from the user ch = input("Choose A, B, or C:") #checking if user entered A, B or C #returning the entered character in this case if (ch=='A' or ch=='B' or ch=='C'): return ch #else printing the invalid option and returning 'A' print("Invalid option, defaulting to A") return 'A'

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Problem C. (10 points) Text Adventure Game

Using the function from the previous problem, write a function adventure() which takes in no arguments, and gives the user a sequence of choices, leading to one of several endings. Since we want this problem to not be impossible to grade, you need to follow a very specific choice structure, namely the one outlined in the flowchart below:

THE PREVIOUS FUNCTION def choice(text, optionA, optionB, optionC): #printing the question print(text)

Each diamond represents a decision, for which you must use your choice function from problem B. Each arrow represents one of the three possible choices for that decision: A, B, or C. Each rounded box represents an ending to the story, which comes in one of two possible types: Good (the user succeeds in some way) or Bad (the user fails). The story itself is entirely up to you, with one exception: you cant use the one in the example below. We will not be grading you based on the storys quality, logic, originality, or grammatical correctness, so if youre lazy and just want to put in empty strings for every single choice, thats fine, but you must follow the choice structure in the flowchart above.

Your function should return the boolean value (not the string) True if the user reached a Good ending, or False if they reached a Bad ending. You can put in a few additional print statements beyond the ones inside the choice function to better explain the story, but this is not required.

Hints:

  • There are two basic ways to do this. You could put in nested if statements for every possible combination of decisions, which will result in some repeated code, since there are multiple paths to decision point 4. Or, you could treat this as a state machine. In particular, if we label each decision as shown below, youll notice that every path through the flowchart passes through states (decisions) in an increasing order (a path may skip some states, but it will never go from a higher numbered state, down to a lower numbered one).

  • #printing the options print("A.", optionA) print("B.", optionB) print("C.", optionC) #reading the option

  • You can take advantage of the above by using an extra variable to track what state the user is currently in (what number decision point they are currently at), and changing it every time they make a choice. For each state starting at 1 and ending at 4, check whether the user is in that state, and only give them the respective choice if they are. Since all paths go through states in an increasing numerical order, you only have to check each state once.

    • (If you dont understand the idea behind a state machine, dont worry: its not necessary for this problem or this class, it just makes things a little cleaner)

Constraints:

  • Your submitted hw04.py file should have no code outside of function definitions aside from comments.

  • You must use the choice() function from problem B for each decision.

  • There should be no calls to the input() function anywhere in the code for adventure(): choice already takes in user input, so if you need to take in more user input then youre not using choice correctly.

Examples (text in bold is returned, text in red is user input, text in italics is printed. Note that because you are required to have a different story than mine, the printed text should not match, but the same sequence of inputs should lead to the same return value):

>>> adventure()

You sneak into the dragon's lair, with your comrades Wizard McBlastyFace and Stella the Bard. The dragon is fast asleep.

A. Tell your team to start stealing things

B. Tickle the dragon on the nose

C. Tell your team to prepare for battle

Choose A, B, or C: B

The dragon sneezes out a fireball and incinerates you instantly.

False

>>> adventure()

You sneak into the dragon's lair, with your comrades Wizard McBlastyFace and Stella the Bard. The dragon is fast asleep.

A. Tell your team to start stealing things

B. Tickle the dragon on the nose

C. Tell your team to prepare for battle

Choose A, B, or C: D

Invalid option, defaulting to A

You can't carry the entire hoard of loot

A. Take the pile of silk in the corner

B. Take as much gold as you can carry

C. Fight the dragon

Choose A, B, or C: B

Stella trips over a rock and the dragon wakes up.

A. CHARGE!

B. Tell Stella to sing to the dragon

C. Run away

Choose A, B, or C: A

The dragon is so surprised by the stupidity of your attack that it dies of laughter.

True

>>> adventure()

You sneak into the dragon's lair, with your comrades Wizard McBlastyFace and Stella the Bard. The dragon is fast asleep.

A. Tell your team to start stealing things

B. Tickle the dragon on the nose

C. Tell your team to prepare for battle

Choose A, B, or C: C

Who will lead the attack?

A. Wizard McBlastyFace

B. Stella the Bard

C. Me, of course

Choose A, B, or C: A

Wizard McBlastyFace aims a spell and misses horribly, hitting the ceiling. Rocks fall, everyone dies.

False

>>> adventure()

You sneak into the dragon's lair, with your comrades Wizard McBlastyFace and Stella the Bard. The dragon is fast asleep.

A. Tell your team to start stealing things

B. Tickle the dragon on the nose

C. Tell your team to prepare for battle

Choose A, B, or C: A

You can't carry the entire hoard of loot

A. Take the pile of silk in the corner

B. Take as much gold as you can carry

C. Fight the dragon

Choose A, B, or C: C

Who will lead the attack?

A. Wizard McBlastyFace

B. Stella the Bard

C. Me, of course

Choose A, B, or C: B

Stella trips over a rock and the dragon wakes up.

A. CHARGE!

B. Tell Stella to sing to the dragon

C. Run away

Choose A, B, or C: B

The dragon approves of the song, and lets you take whatever loot you can carry as a tip.

True

A . A B Bad Bad Good B Good B A, B Good Bad A 1 A Bad Bad A 2 3 Good B Good B 4 A, B C Good Bad A . A B Bad Bad Good B Good B A, B Good Bad A 1 A Bad Bad A 2 3 Good B Good B 4 A, B C Good Bad

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!