Chapter 6: Alien Encounters (If, Elif, Else Statements)
Welcome back, explorer! We’ve plotted our course, calculated our fuel levels, and are now en route to Mars. In this chapter, we’ll prepare for unexpected encounters in our journey — specifically, alien encounters. Will they be friendly? Will they be hostile? We can’t predict the future, but we can prepare our Python code to respond appropriately using conditional statements. In Python, we do this using the if, elif, and else keywords.
Making Decisions (If Statements)
In Python, an if statement is used to test a specific condition. If the condition is true, the indented block of code below the if statement is executed. If the condition is not true, the code is skipped over. Here’s an example:
1 alien_detected = True
2
3 if alien_detected:
4 print("An alien has been detected!")
In this example, alien_detected is a variable that stores a boolean value (True or False). The if statement checks if alien_detected is True. If it is, “An alien has been detected!” is printed to the screen. If alien_detected is False, the print statement would be skipped over.
Choosing Between Alternatives (Elif and Else Statements)
In some situations, you may need to check multiple conditions and choose between several alternatives. This is where elif and else statements come in handy.
The elif keyword is short for “else if”. It allows you to check multiple expressions for True and execute a block of code as soon as one of the conditions evaluates to True. Similar to else, the elif statement is optional. However, unlike else, there can be an arbitrary number of elif statements following an if.
The else keyword catches anything that isn’t caught by the preceding conditions.
Here’s an example of how you might use these statements in the context of an alien encounter:
1 alien_detected = True
2 alien_friendliness = 7
3
4 if alien_detected:
5 print("An alien has been detected!")
6
7 if alien_friendliness > 5:
8 print("The alien seems friendly. Let's say hello!")
9 elif alien_friendliness > 2:
10 print("The alien seems neutral. Proceed with caution.")
11 else:
12 print("The alien seems hostile. Activate defense systems!")
In this code, we first check if an alien has been detected. If it has, we then assess its friendliness. If the alien_friendliness is greater than 5, we assume the alien is friendly and print a corresponding message. If the alien_friendliness is 5 or less but greater than 2, we assume the alien is neutral and advise caution. If none of the above conditions are met, which in this case means the alien_friendliness is 2 or less, we assume the alien is hostile and recommend activating the defense systems.
Project: Alien Encounter System
Now that you understand how if, elif, and else statements work, let’s use them in a project. Your task is to build an Alien Encounter System for your spacecraft. Here’s what your program should do:
Start with the assumption that no alien has been detected and the alien friendliness level is unknown.
Randomly decide if an alien has been detected (you can use the
randommodule for this, which we’ll cover in a later chapter, or for now, change the variables manually to test different scenarios).If an alien is detected, randomly assign a friendliness level to the alien, say, between 0 and 10.
Based on the friendliness level, print out an appropriate message. You can decide what the thresholds should be!
As always, you’re welcome to expand this project as you see fit. For instance, you could introduce more nuances in the alien behavior and response, or add more complexity to the friendliness level.
In the next chapter, we’ll introduce loops, which allow us to execute a block of code multiple times. This will be helpful for tasks like monitoring the spacecraft systems or scanning for alien life continuously. Until then, keep practicing your conditional statements and enjoy your journey through space!