Let's turn this passage into a function.
A function is a chunk of code that does one specific task. In this case, we'll make a function that shows the text, waits a second, shows the options and then goes off to the next passage depending on what the user chooses.
A function is made, or defined, by using the define command.
Enter def followed by the name of the function you are making, then an open and close parentheses, then a colon. Like this: def intro():
We could call our starting passage whatever we like, so you might have def start(): or def beginning(): or def whateveryouwanttocallit(): depending on your style.
After we define the function, let's go down to the next line and indent by pressing the tab key once. Here we'll make text appear on the screen with the print command. The print command works as follows print ("TEXT YOU WANT TO SHOW"). In this case we want to show the text from the passage.
Notice how we made the print command go over 3 lines? That makes it a bit easier to read. To do that, just end the line with a ", then return to the next line and start with another ".
Don't forget to end the print with the closing ). That's a classic programming error, or bug. Remember, if you open something you must close it!
Just wait a second
The next thing we want to do is pause 1 second before displaying the choices. To do this, we'll use the line time.sleep(1).
See how we've added a comment in the same line as this command? That's called an in-line comment and it's a super good idea. Use comments as many places as possible to make it easy to understand your code.
time.sleep() is a special function that requires what's called a module. In order to use a module, we have to import it which we'll do in the next step...
Import time
Above the function, import the time module by typing import time.
Behind the scenes this brings in a bunch of complicated code so Python knows what a second is. We won't worry too much about that, but give a quiet thank you to whomever created the time module and just saved you a ton of work.
The important thing (get it) is that if we use a function from a module, we must import it at the beginning of our program.
Give me options
Now that we've shown the text and waited a second, let's show the options.
To do this, use a print command.
Notice how this print command uses a different option for spanning multiple lines? If you surround your print text, which is called a string, in tripple quotes ("""), you can span multiple lines. It's another way to span lines like we did before. Choose whichever feels right to you. Listen to your heart.
Don't forget to use inline comments - they are super helpful!
Input please
Now we need to get input from the user.
To do this, we'll create a variable, which is a bucket in which to save something. We'll call it choice, and that's where the user's choice will be saved.
We'll set that equal to a function called input(). Input gives the user a chance to type something in.
So here, we are asking the user to type something, and we are saving it as choice.
Choices
Now'll we'll look at what the user said. If it's A, we'll go to the function that matches that choice (which we'll make later).
To do this we'll use the if statement. An if statement looks to see if something is true, and if it's true it'll do something.
Here we setup the statement like so: if choice in answer_a: Remember, choice is where we are saving the answer from the user, and answer_A is a list of acceptable answers that'll match as A. We'll set that list up soon.
After the if line, we indent (use tab) and then enter the name of a function we'll make for that choice. In this case, if they select A they will be stopping the colonists, so we named the function option_stop_colonists(). You can name the functions as you see fit.
Adding other choices
Now that we've checked if the user chooses A, we'll setup the next if statement for B.
Here we'll use an elif - this is Else IF, which means if it wasn't A, let's check if it's something else.
The line will be elif choice in answer_B: answer_B is a list of acceptable choices that'll match for B.
Then we name a function we'll make later that will be the passage for that option. In this case B matches with waiting, so we named the function option_wait(). You can name the function as you see fit.
What if they didn't choose A or B
Finally we need to check if they didn't choose A or B. To do this we'll use an else: which says anything other than what we looked for above will be matched as true. So if it's anything else, we'll want to print some instructions on the options. we will save those instructions as required, so for now we'll add else: and then indented on the next line, print (required).
Finally we'll want to give them the passage again, so we'll run this function again - intro()
Possible Answers
At the top of our program, after the import, we'll want to make a few lists of what answers match as A, B, C, etc.
Here we create a variable named answer_A, answer_B, etc, and set them equal to what the user could type to indicate that choice. A list starts with a bracket [, has each item in "quotations", separated by a comma ,.
Required Text
Next we'll create a variable that has the instructions if they choose something we don't expect.
To do this we'll add required = (\nUse only A, B, or C\n")
Notice how the start and end of the string have \n in them? That starts a new line and can be helpful in adding some space to your text.