3.5 code practice input a grade number (9-12) and print Freshman, Sophomore, junior, senior. if it is not in [9-12] , print not in highschool.
The correct answer and explanation is:
Here’s the correct Python code for the problem:
grade = int(input("Enter your grade (9-12): "))
if grade == 9:
print("Freshman")
elif grade == 10:
print("Sophomore")
elif grade == 11:
print("Junior")
elif grade == 12:
print("Senior")
else:
print("Not in high school")
π Explanation (Approx. 300 words)
This simple Python program takes a grade level input from the user and prints the corresponding high school classification. The recognized grade levels in U.S. high schools are typically grades 9 through 12, and each corresponds to a specific title:
- 9th grade = Freshman
- 10th grade = Sophomore
- 11th grade = Junior
- 12th grade = Senior
β How the Code Works:
- Input Collection:
grade = int(input("Enter your grade (9-12): "))
This line asks the user to enter a number representing their grade. Theinput()
function reads the input as a string, andint()
converts it to an integer for comparison. - Conditional Logic:
The program uses a series ofif
,elif
, andelse
statements to determine which title to print based on the value ofgrade
:- If
grade == 9
, it prints"Freshman"
. - If
grade == 10
, it prints"Sophomore"
. - If
grade == 11
, it prints"Junior"
. - If
grade == 12
, it prints"Senior"
. - If none of these conditions match (i.e., the number is not 9, 10, 11, or 12), the
else
block runs, printing"Not in high school"
.
- If
π§ Why Itβs Important:
This practice helps students understand how conditional statements (if
, elif
, else
) work in Python. It also shows the importance of data validation, since any number outside the 9β12 range is caught by the else
statement.
This type of logic is commonly used in real-world programs to map numeric input to specific categories or responses.