Level 3: File Handling - IncrediCoders/Python1 GitHub Wiki

GitHubAvatar copy

Annie Conda added this page on March 15, 2025


Here is a bonus article that will tell you more about File Handling!

In addition to this bonus article, you can find other bonus articles that teach you the topics I covered in Level 3: Lists, Arguments, and While Loops.

Learn About File Handling

File handling means your code can open, read, write, and save information in a file. It's how programs can remember things even after they stop running.

Imagine writing your homework in a notebook: when you're done, you close it. Later, you can open it again and keep working. Code can do the same with files!

Why File Handling Matters

Sometimes, your program needs to save things like scores, notes, or settings—even after it's turned off. That’s when file handling comes in handy! It helps your program remember what happened before.

Python Example

In Python, saving and reading a file is easy. You just tell the computer what file to open, whether you want to write or read, and what to do.

Scenario: Paul Python wants to save his favorite animal in a file.

# Save to a file
with open("favorite.txt", "w") as file:
    file.write("Turtle")

# Read from the file
with open("favorite.txt", "r") as file:
    print(file.read())

What this does:

  • w means write to the file (it will save "Turtle").
  • r means read from the file (it will show what’s inside).

Comparing Programming Languages

Every programming language can save and read files, but they look a little different when they do it.

Scenario: We want to save the word "Hello" in a file called greeting.txt.

Python

with open("greeting.txt", "w") as file:
    file.write("Hello")

Java

import java.io.FileWriter;

public class Main {
  public static void main(String[] args) throws Exception {
    FileWriter file = new FileWriter("greeting.txt");
    file.write("Hello");
    file.close();
  }
}

C#

using System.IO;

class Program {
  static void Main() {
    File.WriteAllText("greeting.txt", "Hello");
  }
}

What’s the Difference?

  • Python is the shortest and easiest. It uses just a couple of lines.
  • Java and C# are more detailed, and you need to write a little more.

Learn More

TBD

Next Steps

Next, you can take on the two extra challenges and a bonus challenge to add to your Classroom Quiz program and learn more! When you're done, you can move on to Level 4, the Space Wars game!

Take the Challenges!

  1. Challenge 1: Add a score to the game, so you can try to get all the questions right. When the game ends, display the score!

  2. Challenge 2: Make the text of the answer choices turn red and green to show which one is the correct answer, as a visual feedback effect.

  3. Bonus Challenge: First, you're going to load in a new quiz with more questions! Then, you're going to add in some of your own quiz questions! Finally, you're going to make your own quiz about whatever you want!

More Level 3 Resources

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

  • Level 3: Online Articles - I made you a list of different web pages I found, which will help you learn more about creating a Classroom Quiz.

  • Level 3: 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 3: Unplugged Activity - I wrote this page with more details than what you saw in the book. In this game, you'll have one person act as the developer, and one person act as the variable where the developer chooses a noun for the variable to say in the story that they tell!

  • Level 3: Rewards - If you completed the Class Introductions project that we talked about, then I set up this page to act as a reward. You can see some illustrations of me and learn more about who I am! You'll also find the Apple Award digital download, to show off your accomplishment!

Level 4

After you're completely done with Level 3 (did you do the challenges?), then it's time to move on to Level 4! While you read through Level 4 in your book, you can check out the resources from SideWinder, as she teaches you how to build the Space Wars game:

I hope you had fun learning about Classroom Quiz! This is something that is very important for all of the future levels! Enjoy!

-- Annie

⚠️ **GitHub.com Fallback** ⚠️