Level 4: Elif - IncrediCoders/Python1 GitHub Wiki

SW_avatar7

SideWinder added this page on June 6, 2025

Hi Paul, and I guess whoever else is reading this! I wrote you this bonus article about Elif, because I'm like the best friend ever!

In addition to this bonus article, you can find another bonus article on topics I showed you in Level 4: For Loops and States

Learn about For Elif

In programming, we use conditions to make choices. We have already discussed if and else previously. But what if you have more than two choices?

That’s where elif comes in. It means “else if” and helps your program check more than one condition.

Why Elif Matters

elif is useful when you want your code to try different paths until one fits. It helps your program decide what to do in more complex situations, like grading tests or choosing what to wear based on the weather. For example, if it’s hot, Paul Python will wear shorts. If it’s cold, he'll wear a jacket. If it’s rainy, he'll take an umbrella.

Python Example

In Python, you can use if, elif, and else together to check several different conditions.

Scenario: Paul Python wants to decide what to bring to school based on the weather.

weather = "rainy"

if weather == "sunny":
    print("Bring sunglasses!")
elif weather == "rainy":
    print("Bring an umbrella!")
else:
    print("Just bring a backpack.")

What this means:

  • If the weather is sunny, bring sunglasses.
  • If it’s rainy, bring an umbrella.
  • Otherwise, just bring a backpack.

The program checks each condition in order and runs the first one that is true.

Comparing Programming Languages

Different languages check conditions in different ways. But they all let you say: “If this happens, do that. If something else happens, do something else.”

For example: We want to print a message based on someone’s score:

  • If score is 90 or higher: “Great job!”
  • If score is 70 or higher: “Nice work!”
  • Otherwise: “Keep practicing!”

Python

score = 85

if score >= 90:
    print("Great job!")
elif score >= 70:
    print("Nice work!")
else:
    print("Keep practicing!")

Java

int score = 85;

if (score >= 90) {
    System.out.println("Great job!");
} else if (score >= 70) {
    System.out.println("Nice work!");
} else {
    System.out.println("Keep practicing!");
}

C#

int score = 85;

if (score >= 90) {
    Console.WriteLine("Great job!");
} else if (score >= 70) {
    Console.WriteLine("Nice work!");
} else {
    Console.WriteLine("Keep practicing!");
}

What’s the Difference?

  • Python uses elif (short for “else if”). You need to include a colon at the end of the elif statement/line of code, and the code in the block needs to be indented under the elif statement.
  • Java and C# both use else if with parentheses and curly braces.
  • All three languages let you check multiple conditions and choose what to do.

Learn More

TBD

Next Steps

Next, you can take on the two extra challenges to add to your Space Wars program and learn more! When you're done, you can move on to Level 5, the Creeper Chase!

Take the Challenges!

  1. Challenge 1: In this challenge, you are going to add sound to the game when projectiles are fired!

  2. Challenge 2: In this challenge, you are going to write code that will add asteroids to the game!

More Level 4 Resources

In addition to this Help page and the instructions for our Level 4 challenges, we also have Online Articles, a Learning Quiz, an Unplugged Activity, and a Rewards article:

  • Level 4: Online Articles - I made you a list of different web pages I found, which will help you learn more about creating the Space Wars program.

  • Level 4: Learning Quiz - I wrote some questions in case you want to quiz yourself about what you learned. Or you can teach others and quiz them!

  • Level 4: Unplugged Activity - I wrote this page with more details than what you saw in the book. You're going to recreate the SpaceWars game in person! You'll get a chance to review and practice key events and how they work with spaceships.

  • Level 4: Rewards - If you completed the Space Wars project, then you're welcome to check out this page as a reward. You can see some illustrations of me and learn more about who I am! You'll also find the Goth Award digital download, to show off your accomplishment!

Level 5

After you're completely done with Level 4 (did you do the challenges?), then it's time to move on to Level 5! While you read through Level 5 in your book, you can check out the resources from Intelli-Scents, as she teaches you how to build the Creeper Chase program:

I hope you had fun learning about space wars!

-- SideWinder