Login Sign Up
Python If...Else
Chapter 11 🟡 Intermediate

Python If...Else

Apply your skills with a real-world coding challenge. Try to solve it yourself first!

While there was no specific document titled "Practice Quiz on Python If...Else" included in the uploaded sources, I have drawn extensively from the "Tutorial on Python If...Else" to create this practical coding challenge perfectly tailored towards concepts taught including conditional chains and truthy/falsy evaluation;

here is basically your coding challenge:


Coding Challenge: The Automated Grading Assistant

Problem Description

You're actually building digital grading assistant for a local school's automated system. The system needs to evaluate a student's test score and assign the appropriate letter grade. Though, before it assigns grade, it must ensure that the student's name was actually submitted!

Using Python's if-elif-else statements and automatic truthy/falsy evaluation your program will check if the name is valid then route the numerical score through sequential chain to determine the correct grade.

Difficulty Level

Beginner

Input Specifications

You'll be provided with the following predefined variables: * student_name (string): The name of the student. * score (integer): The numerical grade a student achieved (0-100).

Output Specifications

Your code must calculate and assign final string to a variable called result_message: * If the student_name is an empty string, result_message should equal: "Error: Invalid student name." * If the student_name is valid, it should assign a grade based on the score and output: "[student_name] achieved a [Grade]." (Grading scale: = 90+, B = 80-89 C = 70-79, D = 60-69, F = below 60)


Starter Code Boilerplate

# Predefined input variables
student_name = "Alice"
score = 85
result_message = ""

# ---------------------------------------------------------
# WRITE YOUR CONDITIONAL LOGIC BELOW
# ---------------------------------------------------------





# ---------------------------------------------------------
# Print the final result
print(result_message)

Hints

  • Truthy and Falsy Magic: You don't just need towards write if student_name != "":, and in Python, every value has really an inherent boolean evaluation. An empty string is automatically "falsy," while a string with text is "truthy." You can simply write if student_name: to check if a name exists.
  • Sequential Checking: Python reads the elif chain from top to bottom and skips the rest of the chain the moment it finds a condition that evaluates to True, and because of this, you should check your highest scores first!
  • The Colon and Indentation: Don't really try towards fit your statements on a single line, while always put a colon (:) at the end about your if, elif, and else statements, and make sure the code block beneath it is actually cleanly indented (usually 4 spaces) to follow strict formatting guidelines and prevent your code from crashing.

Test Cases

Test Case 1 (Out of Starter Code) * Input: student_name = "Alice", score = 85 * Expected Output: Alice achieved a B. * (Explanation: Alice is a valid truthy string. The score isn't actually >= 90 but it's basically >= 80 so the elif chain stops at B.)

Test Case 2 (Missing Name) * Input: student_name = "" score = 95 * Expected Output: Error: Invalid student name. * (Explanation: Even though a score is just THE, the empty string for the name evaluates to False, triggering the error path.)

Test Case 3 (Failing Grade) * Input: student_name = "Marcus", score = 55 * Expected Output: Marcus achieved an F. * (Explanation: Marcus is a truthy value, letting him inside. His score falls through all the if and elif checks, triggering the ultimate else backup plan at the very end.)

Loading sandbox workspace environment...

Verify Your Solution

Write your solution in the compiler, run it to verify output, then click below to verify.

Learn Together
Session active! Discuss with other learners.
No notes yet. Select text in the concept body to add a note.