Fall 2024 - P3
3.7.2 Python Hack For Nested Conditionals
grade determiner in python
POPCORN HACK: Python Example of Nested Conditionals!
# Function to determine the grade based on a score
def determine_grade(score):
if score >= 90:
return "A"
elif score >= 80:
return "B"
elif score >= 70:
return "C"
else:
# Nested conditionals for borderline cases
if score >= 60:
print("Borderline case: Checking additional criteria...")
extra_credit = input("Did the student complete extra credit? (yes/no): ").lower()
if extra_credit == "yes":
return "C"
else:
return "D"
else:
return "F"
# Get user input for the score
try:
score = int(input("Enter the student's score (0-100): "))
if 0 <= score <= 100:
grade = determine_grade(score)
print(f"The student's grade is: {grade}")
else:
print("Please enter a valid score between 0 and 100.")
except ValueError:
print("Invalid input! Please enter a numeric value.")