Easy ยท ages 11+ ยท lists ยท functions ยท if / elif
Marks and grades in Python.
Find the total, average and grade for a list of marks.
The program
Press Run to see what it prints. Change anything you like, then run it again.
marks = [78, 92, 65, 88, 71]
def grade(percent):
if percent >= 90:
return "A"
elif percent >= 75:
return "B"
elif percent >= 60:
return "C"
return "D"
total = sum(marks)
average = total / len(marks)
print("Total:", total)
print("Average:", round(average, 1))
print("Grade:", grade(average))
print("Highest:", max(marks), "Lowest:", min(marks))
print("Sorted:", sorted(marks))Total: 394 Average: 78.8 Grade: B Highest: 92 Lowest: 65 Sorted: [65, 71, 78, 88, 92]
How it works
sum,max,min,lenandsortedare built-in functions that work on lists.- The
gradefunction checks from the highest boundary down, so the first test that is true decides the grade. round(average, 1)rounds to one decimal place.
Try this
- Use your own marks.
- Change the grade boundaries.
- Print which subjects are above the average.
Stuck on an error? Read how to debug Python.
Similar ideas
More programs
Fibonacci numbers
Build the Fibonacci sequence, where each number is the sum of the two before it.
Easy ยท ages 10+Temperature converter
Turn Celsius into Fahrenheit and print a conversion table.
Beginner ยท ages 9+Palindrome checker
Check whether a word reads the same forwards and backwards.
Easy ยท ages 10+FAQ
Marks and grades: questions
What does the marks and grades program do?
Find the total, average and grade for a list of marks. It prints: Total: 394 / Average: 78.8 / Grade: B โฆ
Which Python ideas does it use?
lists, functions, if / elif. Easy level, suitable from about age 11.
How do I run it?
Press Run on this page. To run it on your own computer, install Python 3 and save the code in a file ending in .py. It prints the same thing.
What should I try next?
Use your own marks. Change the grade boundaries. Print which subjects are above the average.
Start a conversation
Want a teacher to guide the learning?
Tell us the learner's age and what they can already do, and we will suggest a starting point. A parent or guardian can make the enquiry.
- Tell us the learnerโs age or grade and goals
- Weโll suggest a suitable class or learning path
- Weโll confirm format, timing and fees with a parent or guardian
Play it, don't just read it
Write some code right now
Free games that need no sign-up. Pick one and play for five minutes.
Pick a game
- Code BuilderThe lines of a Python program are mixed up. Tap them into the rightโฆPlay
- Predict the OutputRead a short Python program and choose what it prints. Then watch it runโฆPlay
- Bug HunterA Python program prints the wrong answer. Find the one line with theโฆPlay
- Code JungleWrite real Python to guide Miko the monkey to every banana. 24 levels:โฆPlay