If you're looking for general guidance or answers to common questions about Python 2, here are a few areas that might be helpful:
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.
Before looking up specific answers, make sure you understand the "Big Three" concepts introduced in this level. If you understand the logic, the code writes itself. code avengers answers python 2 new
Task: Use a for loop to print the numbers 1 to 5.
Answer:
for i in range(1, 6):
print(i)
Pro Tip: The range function stops before the second number. That's why we use 6 to print up to 5.
Previously, list comprehensions were taught in Python 3 courses only. The new Python 2 curriculum introduces them as an "advanced but preferred" method. If you're looking for general guidance or answers
Prompt:
“Take original = [1, 2, 3, 4, 5] and create a new list squared where each number is squared. Then print squared. Do not use a traditional for loop.”
Answer:
original = [1, 2, 3, 4, 5]
squared = [x**2 for x in original]
print(squared)
Output: [1, 4, 9, 16, 25]
Problem (New version):
Write a guessing game where the secret number is 7. The user has unlimited guesses, but after each wrong guess, print "Too high" or "Too low". If the user types "quit", exit the game immediately. If they guess correctly, print "You win!" and stop. Note: The new curriculum rejects solutions without the
Solution:
secret = 7
while True: guess = input("Guess a number (or 'quit'): ") if guess == "quit": break guess = int(guess) if guess == secret: print("You win!") break elif guess < secret: print("Too low") else: print("Too high")
Why this clears the new validator:
while True (common in intermediate challenges)."quit" before converting to int (otherwise, you get a ValueError).break placement.