Code Avengers Answers Python 2 New

Python 2 Code Avengers Answers

Challenge 1.2: The Exit Sentinel

The Prompt:
“Keep asking the user for a word. If they type 'quit', stop the loop. Otherwise, print the word in uppercase.”

Answer:

word = ""
while word != "quit":
    word = input("Enter a word (or 'quit'): ")
    if word != "quit":
        print(word.upper())

Note: The new curriculum rejects solutions without the if guard, as printing "QUIT" after typing quit is considered a logical error.


🎉 Final Thoughts

Don't be afraid to break your code! In Python Level 2, trial and error is the best way to learn. If you fail a test case, change a number, swap an if for an elif, or check your variables. code avengers answers python 2 new

Got a specific question from a lesson you're stuck on? Drop the problem description in the comments below, and we'll solve it together!

Conditional Statements

Use if, elif, and else for conditional execution.

x = 5
if x > 10:
    print "x is greater than 10"
elif x == 5:
    print "x is equal to 5"
else:
    print "x is less than 10"

1. The if, elif, and else Statements

This is how your program makes decisions.

Sample Question: Write a program that checks if a variable age is 18 or older. If yes, print "Adult". Otherwise, print "Minor". Python 2 Code Avengers Answers Challenge 1

Solution:

if age >= 18:
    print("Adult")
else:
    print("Minor")

Example Code (Replace this with your own):

age = int(input("Enter age: ")) if age >= 18: print("You are an adult.") else: print("You are a minor.") """

# This executes the code in a safe, temporary scope
local_scope = {}
try:
    # 1. Compile the code to check for Syntax Errors first
    compiled_code = compile(user_code, '<string>', 'exec')
# 2. Run the code
    # Note: If your code requires input, you will need to type it in the console here.
    exec(compiled_code, {}, local_scope)
print("\n--- Execution Successful ---")
    print("Check the output above to verify correctness.")
except SyntaxError as e:
    print(f"\n!!! Syntax Error Detected !!!")
    print(f"Error: e")
    print("Common Fixes: Check for missing colons ':', mismatched parentheses '()', or indentation errors.")
except Exception as e:
    print(f"\n!!! Runtime Error Detected !!!")
    print(f"Error Type: type(e).__name__")
    print(f"Details: e")

How to Use Answers Without Cheating (Ethical Learning)

Code Avengers’ platform detects copy-pasting and unusual solve times. To truly learn:

  1. Read the error – The "new" course provides descriptive hints.
  2. Write the code yourself – Even if you look at the answer, type it character by character.
  3. Modify variables – Change the secret number or list values to see if your logic holds.
  4. Explain each line – Use comments to prove understanding.

Tuples

Tuples are ordered, immutable collections of items. Note: The new curriculum rejects solutions without the

# create a tuple
my_tuple = (1, 2, 3, 4, 5)
# access elements
print my_tuple[0]  # prints 1
# trying to modify a tuple will result in an error
try:
    my_tuple[0] = 10
except TypeError:
    print "Tuples are immutable"

Challenge 2.1: Multiplication Table Printer (New Style)

Prompt:
“Print the multiplication table for numbers 1 through 5, from 1 to 10. Use nested for loops. Output should be formatted as '2 x 3 = 6'.”

Answer:

for i in range(1, 6):
    for j in range(1, 11):
        print(f"i x j = i*j")
    print("---")  # Separator between tables

Common mistake: Forgetting the separator line or using break incorrectly.