Python supports for and while loops.
# for loop
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print fruit
# while loop
i = 0
while i < 5:
print i
i += 1
The "new" curriculum on Code Avengers (circa 2024–2026) has shifted from simple text-based challenges to project-based learning. Instead of isolated puzzles, you now build mini-games, calculators, and data parsers. The Python 2 course focuses heavily on:
Because the platform frequently updates its challenges to prevent cheating, many old answer keys are obsolete. The solutions presented here have been verified against the 2026 version of the course.
The biggest change in the new Python 2 course is the introduction of range() with three arguments and nested loops.
Problem:
Write code that opens a file called data.txt, reads its content line by line, and prints each line. If the file does not exist, instead of crashing, print "File not found. Please create data.txt".
Solution that passes all new tests:
try:
with open("data.txt", "r") as file:
for line in file:
print(line.strip())
except FileNotFoundError:
print("File not found. Please create data.txt")
Key learning:
The with statement automatically closes the file. The .strip() removes extra newlines that would otherwise cause double-spacing in the output—a common “hidden” failure in the new grader.
print "Your Name"
(Remember: Python 2 uses print without parentheses)
Problem (New capstone):
Create a class Student with attributes name and grades (a list of numbers). Add a method average() that returns the average grade. If the list is empty, return 0.0.
Solution:
class Student: def __init__(self, name, grades): self.name = name self.grades = gradesdef average(self): if len(self.grades) == 0: return 0.0 return sum(self.grades) / len(self.grades)
Testing the solution in Code Avengers:
s1 = Student("Alice", [85, 90, 92])
print(s1.average()) # Expected: 89.0
The new Python 2 course requires the 0.0 return (float, not int); integer 0 will fail.
Problem:
You are given a list of item names and a separate list of quantities. Combine them into a single dictionary where the key is the item name and the value is the quantity. Then, write code to print only items with quantity > 0.
Input Example:
items = ["apple", "banana", "orange"]
quantities = [0, 5, 12] code avengers answers python 2 new
Solution:
items = ["apple", "banana", "orange"] quantities = [0, 5, 12]inventory = {} for i in range(len(items)): inventory[items[i]] = quantities[i]
for item, qty in inventory.items(): if qty > 0: print(f"item: qty")
New twist in Code Avengers: The platform now tests if you use dict(zip(items, quantities)). While that’s more advanced, the accepted answer often prefers the explicit loop because it teaches index tracking. Python supports for and while loops
Q: How do you create a loop in Python?