Grade Calculator
So far your programs have done all the talking. Now we'll let the user talk back. You'll ask for a number of points and hand back a letter grade — which means reading input, turning text into a number, and making a decision.
Overview
You'll learn to get input from the user, cast that text into a number, and write a function that uses conditionals to turn points into a letter grade.
You've got it when…
- Your program asks the user for a number of points.
- A
calculate_grade()function returns the correct letter grade. - The program prints the letter grade to the screen.
Collaboration & AI
Work: On your own. A neighbor can help you debug; the logic is yours.
AI — AIAS Level 1, No AI: Write the conditionals yourself. What the levels mean.
Getting Input From the User
input() is a built-in
function that pauses your program and waits for the user to type something.
- Whatever the user types comes back as a string (text), even if they type numbers.
- The program continues once the user presses Enter.
How it works:
- Python shows the prompt:
What is your name? - The user types something (say,
Steve) and presses Enter. - The string
"Steve"is stored in the variablename.
Casting
input() always returns a string. To use what the user typed as a number,
first cast it:
int()— makes an integer (whole number) from a string.float()— makes a floating-point (decimal) number from a string.
After working with a number, you may need to cast back to a string to
concatenate it (+) with other text for printing:
str()— makes a string from a number.
age_string = input("What is your age?")
age = int(age_string)
print("In ten years, you will be " + str(age + 10) + " years old.")
The first two lines can be combined by passing input() straight into int():
Grade Calculator
Design a program that calculates a letter grade using this scale:
| Points | Grade |
|---|---|
| 90 to 100 | A |
| 80 to <90 | B |
| 70 to <80 | C |
| 60 to <70 | D |
| <60 | F |
Do this:
- Write a function
with this signature:
calculate_grade(points). - The function expects an integer number of points (the
pointsargument). - Use conditionals
(
if,elif, andelse) with the table above to determine the grade. - The function returns the correct letter grade.
- This function does not use
print(). - Write a main program that asks the user for a number of points with
input(), calls yourcalculate_grade()function, and prints the letter grade it returns.
Turn It In
- Your Python (
.py) file with the grade-calculation program.
How It's Graded
This lab is worth up to 4 points. One score covers everything you turn in.
| Score | What it looks like |
|---|---|
| 4 — Excellent | calculate_grade(points) returns the right letter for every band — including the boundaries 90, 80, 70, and 60 — and never prints. The main program gets the points with input(), casts them, calls your function, and prints what comes back. You tested the edges, not just one number from the middle. |
| 3 — Above Average | The program works end to end, with a minor slip — one boundary off by a point, or a stray print left inside the function. |
| 2 — Average | Grades come out wrong for several bands, the function prints instead of returning, or the conditional logic lives in the main program with no function. |
| 1 — Below Average | The program doesn't run, or there's no conditional logic to grade with. |
| 0 — Failing | Nothing submitted, or no evidence of the calculator. |