ይህ ትምህርት ገና ወደ አማርኛ አልተተረጎመም፤ ስለዚህ በእንግሊዝኛ ቀርቧል። የእንግሊዝኛውን ገጽ ክፈቱ
Project: Grade calculator
Time to put the module together. You'll build a grade calculator: it asks for an exam score, refuses scores that can't be real, and prints the letter grade and grade points. The hard part isn't the code, it's getting every boundary right: is 85 an A or a B? A calculator that's wrong for one score in a hundred is wrong.
The scale here is an example, simpler than most real ones. Universities in Ethiopia use different scales (many have A+, A−, B+, and so on), so check yours and adapt the program once it works.
| Score | Grade | Points |
|---|---|---|
| 85–100 | A | 4.0 |
| 70–84 | B | 3.0 |
| 50–69 | C | 2.0 |
| 0–49 | F | 0.0 |
Here's the finished program with a score of 85:
Score (0-100): 85
Grade: A (4.0 points)Try each milestone before you open the solution.
Milestone 1: the grade chain
Start with a fixed score and an if / elif / else chain. Remember lesson 2.2:
only the first true branch runs, so the order matters.
መልመጃ
Grade a fixed score
Print the letter grade for score. Then change score to 85, 84, 70, 69, 50, and 49,
and check each against the table.
Your code
score = 90
# Print the grade: A, B, C, or FIn the editor, press Escape then Tab to move on.
መፍትሄውን አሳይHide the solution
This is one way to solve it, not the only one. If yours prints the same thing, it works.
score = 90
if score >= 85:
print("Grade: A")
elif score >= 70:
print("Grade: B")
elif score >= 50:
print("Grade: C")
else:
print("Grade: F")Before you move on, break it on purpose: move the score >= 50 branch to the top and
run it with 90. You get a C, the bug from lesson 2.2. Now you'll recognize it when it
happens by accident.
Milestone 2: asking, and saying no
Now ask for the score. A real user will make typos, so check the score is possible before grading it. A score below 0 or above 100 gets a clear message instead of a grade. It can be the first branch of the chain:
መልመጃ
Ask and check the score
Ask for the score with int(input(...)). If it's below 0 or above 100, print
Please enter a score from 0 to 100. Otherwise print the grade. The Input box has
101.
Your code
score = 90
if score >= 85:
print("Grade: A")
elif score >= 70:
print("Grade: B")
elif score >= 50:
print("Grade: C")
else:
print("Grade: F")In the editor, press Escape then Tab to move on.
One answer per line, given to input() in order.
መፍትሄውን አሳይHide the solution
This is one way to solve it, not the only one. If yours prints the same thing, it works.
score = int(input("Score (0-100): "))
if score < 0 or score > 100:
print("Please enter a score from 0 to 100.")
elif score >= 85:
print("Grade: A")
elif score >= 70:
print("Grade: B")
elif score >= 50:
print("Grade: C")
else:
print("Grade: F")Why must the check go first? Predict what this version prints for 101.
ውጤቱን ገምቱ
Decide before you look. Guessing wrong is how this sticks.
score = 101
if score >= 85:
print("Grade: A")
elif score < 0 or score > 100:
print("Please enter a score from 0 to 100.")Python prints
Grade: A
101 >= 85 is True, and it's checked first, so the chain stops there. The check for impossible scores is never reached. Put it first, and every other branch can trust the score.
One kind of bad input still gets through. If someone types a word, int() can't
convert it (lesson 1.5):
Milestone 3: grade points
Now print the points too. Instead of printing in every branch, give two names,
grade and points, a value in each branch, and print once at the end. That print
only makes sense for a valid score, so the grade chain moves inside the else. (This
else holds more than an if, so it can't be flattened into elif.)
መልመጃ
Add the points
In each branch, give grade and points their values. After the chain, print them
as Grade: A (4.0 points). The Input box has 85, the most important boundary.
Your code
score = int(input("Score (0-100): "))
if score < 0 or score > 100:
print("Please enter a score from 0 to 100.")
else:
# Give grade and points a value in each branch, then print them
passIn the editor, press Escape then Tab to move on.
One answer per line, given to input() in order.
መፍትሄውን አሳይHide the solution
This is one way to solve it, not the only one. If yours prints the same thing, it works.
score = int(input("Score (0-100): "))
if score < 0 or score > 100:
print("Please enter a score from 0 to 100.")
else:
if score >= 85:
grade = "A"
points = 4.0
elif score >= 70:
grade = "B"
points = 3.0
elif score >= 50:
grade = "C"
points = 2.0
else:
grade = "F"
points = 0.0
print(f"Grade: {grade} ({points} points)")pass in the starter means "do nothing": a block can't be empty, so it holds the place
until you write the real lines. Delete it when you do.
Test every boundary
A grade calculator is only right if it's right at the edges. Type each score in the
Input box and check your output against this table, character by character. Every
row is a place where a > instead of >=, or a wrong order, would show up.
| You type | Your program prints |
|---|---|
-1 | Please enter a score from 0 to 100. |
0 | Grade: F (0.0 points) |
49 | Grade: F (0.0 points) |
50 | Grade: C (2.0 points) |
69 | Grade: C (2.0 points) |
70 | Grade: B (3.0 points) |
84 | Grade: B (3.0 points) |
85 | Grade: A (4.0 points) |
100 | Grade: A (4.0 points) |
101 | Please enter a score from 0 to 100. |
Milestone 4: a weighted GPA (stretch)
A GPA weighs each course by its credit hours: multiply each course's points by its credit hours, add them up, and divide by the total credit hours. A 3-credit A counts more than a 2-credit C.
Turning three scores into points would mean writing the grade chain three times. In Module 3 you'll learn to repeat code without copying it, so for now, ask for each course's points directly (your milestone 3 program tells you them).
መልመጃ
Weighted GPA (stretch)
Ask for the points and credit hours of three courses, then print the GPA with two decimals. The Input box has an A in a 3-credit course, a B in a 4-credit course, and a C in a 2-credit course.
Your code
points1 = float(input("Course 1 points: "))
credits1 = int(input("Course 1 credit hours: "))
# Ask for courses 2 and 3, then work out and print the GPAIn the editor, press Escape then Tab to move on.
One answer per line, given to input() in order.
መፍትሄውን አሳይHide the solution
This is one way to solve it, not the only one. If yours prints the same thing, it works.
points1 = float(input("Course 1 points: "))
credits1 = int(input("Course 1 credit hours: "))
points2 = float(input("Course 2 points: "))
credits2 = int(input("Course 2 credit hours: "))
points3 = float(input("Course 3 points: "))
credits3 = int(input("Course 3 credit hours: "))
weighted = points1 * credits1 + points2 * credits2 + points3 * credits3
total_credits = credits1 + credits2 + credits3
print(f"GPA: {weighted / total_credits:.2f}")The plain average of 4.0, 3.0, and 2.0 would be 3.00. The weighted GPA is 3.11, because the A carries three credits and the C only two.
ዋና ዋና ነጥቦች
- Build in milestones: a fixed value first, then input, then the extras.
- Order a grade chain from the highest grade down, and check that a score is possible before grading it.
- Give names a value in each branch and print once at the end, instead of printing everywhere.
- Test every boundary (49 and 50, 84 and 85) and the impossible values (-1 and 101).