3.1 Variables & Assignments hacks - Python
Apply your understanding of Variables & Assignments with these Python hacks
🐷 Peppa Maze Variables & Assignments Hacks (Python)
Welcome to the Peppa Maze hacks! These challenges will test your understanding of variables, assignments, and logic in Python. Read each task, then write or modify code to solve it.
Hack 1: Python - Variable Assignment, Naming, and Operators
Create variables for Peppa’s name, score, and level using good Python naming conventions. Assign initial values, then use operators to update score (add 10) and level (multiply by 2). Print all results.
# Write your code here
# Create variables for Peppa's name, score, and level
peppa_name = "Peppa"
peppa_score = 50
peppa_level = 3
# Update score (add 10)
peppa_score += 10
# Update level (multiply by 2)
peppa_level *= 2
# Print all results
print("Name:", peppa_name)
print("Score:", peppa_score)
print("Level:", peppa_level)
Name: Peppa
Score: 60
Level: 6
Hack 2: Python - Variable Declaration, Assignment, and Operators
Declare variables for Peppa and George’s scores using good Python naming conventions. Assign initial values, then use operators to update both scores (e.g., Peppa gets 15 points, George loses 5 points). Print both scores.
# Write your code here
# Declare variables for Peppa and George's scores
peppa_score = 40
george_score = 30
# Update scores: Peppa gets +15, George loses -5
peppa_score += 15 # shorthand for peppa_score = peppa_score + 15
george_score -= 5 # shorthand for george_score = george_score - 5
# Print both scores
print("Peppa's Score:", peppa_score)
print("George's Score:", george_score)
Peppa's Score: 55
George's Score: 25
Hack 3: Python - Multiple Assignment and Math Operators
Peppa and George both start at level 1. Use Python’s multiple assignment feature to assign both their levels to 5 in one line. Then, calculate a combined score by multiplying their levels together and multiplying by 10. Print all results.
# Write your code here
# Assign starting levels
peppa_level = 1
george_level = 1
# Assign both levels to 5 in one line using multiple assignment
peppa_level = george_level = 5
# Calculate combined score
combined_score = peppa_level * george_level * 10
# Print all results
print("Peppa's Level:", peppa_level)
print("George's Level:", george_level)
print("Combined Score:", combined_score)
Peppa's Level: 5
George's Level: 5
Combined Score: 250