The first beginner Python project on our list is the text adventure game! This project will teach you a lot about inputs, variable data, strings, and integers. The first step of the text adventure is to welcome our player! You can do this by typing:
print('Welcome to Text Adventure!')
Explanation: Next, we need to know our player's name. We can do this using something called an input variable. Input variables take input from a user and store it. You can call a variable by typing its name. The line of code needed to make is:
name = input('Enter your username here. ')
Explanation: "name" is the variable name. We define it as whatever the user inputs, hence the input('Enter your name here:') The next step is to introduce the player to their surroundings. This is where you can get creative. We decided to go with the player being in a cave. You can tell the player what is happening with the print function.
print('You find yourself in a cave. It also smells like fish.')
Explanation: You can use multiple print functions and print numerous lines of text.
print('Suddenly, you hear a thumping noise in the distance. It gets louder and louder.')
run_stay = input('Will you run from the thumping, or will you stay put in the cave? ')
Explanation: Now, the player must make a choice, to either run away or stay put in the cave. We can do this by using input variables and if statements. "If statements" trigger a certain thing to happen when an expectation is met. In this case, the condition is if the user inputs "run."
if run_stay == 'run':
print('You quickly run out of the cave.')
print('Outside, there's a forest stretching miles in every direction, and bones littered all across the floor. ')
Explanation: This is called an if statement, and it has many rules to its syntax. First, the two equal signs are essential. One equal sign is mostly used to assign a value to a variable as we did with our input variables. Two equal signs are used to check values like we are doing in our if statement. The colon at the end of the line states the end of the if statement's conditions. Most python text editors auto-indent the line when you press enter, but if they don't, make sure you indent the line after the "if statement" conditions.
Checkpoint: Now, try running your code! When there's an input variable, you can type in an answer. As you can see, there can be multiple input variables and print functions to make an entire story! Try experimenting with these different ideas and see what you can make!
Comments