0% found this document useful (0 votes)
1 views4 pages

Python Control Structures Answers

The document is an answer key for a Python practice worksheet covering topics such as If/Elif/Else statements, Match statements, For loops, and While loops. It includes sample code for various exercises like age classification, temperature checking, traffic light systems, and password checking. Each part provides clear examples to help learners understand the concepts and syntax of Python programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views4 pages

Python Control Structures Answers

The document is an answer key for a Python practice worksheet covering topics such as If/Elif/Else statements, Match statements, For loops, and While loops. It includes sample code for various exercises like age classification, temperature checking, traffic light systems, and password checking. Each part provides clear examples to help learners understand the concepts and syntax of Python programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

✅ Python Practice Worksheet - Answer Key

Topic: If/Elif/Else, Match, For Loop, While Loop

Part A: If / Elif / Else

Q1: Age Group Classifier


Sample Answer:

python
age = int(input("Enter your age: "))

if age <= 12:


print("You are a child.")
elif age <= 19:
print("You are a teenager.")
elif age <= 59:
print("You are an adult.")
else:
print("You are a senior citizen.")

Q2: Temperature Checker


Sample Answer:

python
temp = int(input("Enter the temperature in °C: "))

if temp < 15:


print("It’s cold.")
elif temp <= 30:
print("It’s warm.")
else:
print("It’s hot.")

Part B: Match (Python 3.10+)

Q1: Traffic Light System


Sample Answer:

python
color = input("Enter traffic light color: ")
match [Link]():
case "red":
print("Stop.")
case "yellow":
print("Slow down.")
case "green":
print("Go!")
case _:
print("Invalid color.")

Q2: Grade Meaning


Sample Answer:

python
grade = input("Enter your grade (A-F): ").upper()

match grade:
case "A":
print("Excellent")
case "B":
print("Good")
case "C":
print("Average")
case "D":
print("Poor")
case "F":
print("Fail")
case _:
print("Invalid grade.")

Q3: Shape Sides


Sample Answer:

python
shape = input("Enter a shape (circle, square, triangle): ").lower()

match shape:
case "circle":
print("A circle has no straight sides.")
case "square":
print("A square has 4 sides.")
case "triangle":
print("A triangle has 3 sides.")
case _:
print("Unknown shape.")

Part C: For Loop

Q1: Counting Numbers


Sample Answer:

python
for i in range(1, 11):
print(i)

Q2: Multiplication Table


Sample Answer:

python
num = int(input("Enter a number: "))

for i in range(1, 11):


print(num, "x", i, "=", num * i)

Q3: Even Numbers


Sample Answer:

python
for i in range(1, 51):
if i % 2 == 0:
print(i)

Part D: While Loop

Q1: Counting with While


Sample Answer:

python
i=1
while i <= 10:
print(i)
i += 1
Q2: Exit Program
Sample Answer:

python
while True:
user_input = input("Enter something (type 'exit' to stop): ")
if user_input.lower() == "exit":
break

Q3: Password Checker


Sample Answer:

python
password = ""

while password != "python123":


password = input("Enter password: ")

print("Access granted!")

You might also like