Swift Gradebook Project
A gradebook project is a small program that stores student scores and prints a report for each student. It brings a dictionary, a struct, a function, and loops together in one program.
Names in sorted order
The scores dictionary pairs three names with their scores. The loop walks the sorted keys, so the names print in the same order every run.
let scores = ["Ana": 92, "Bo": 74, "Cy": 85]
for name in scores.keys.sorted() {
print(name)
}A letter for a score
The grade function switches over score ranges, giving A from 90 up and B from 80 up. The two calls print A for 92 and B for 85.
func grade(for score: Int) -> String {
switch score {
case 90...100:
return "A"
case 80..<90:
return "B"
case 70..<80:
return "C"
default:
return "F"
}
}
print(grade(for: 92))
print(grade(for: 85))One report line from a struct
The Student struct holds a name and a score, and its report method prints one line. The line shows the name, the score, and the letter from the grade function.
func grade(for score: Int) -> String {
switch score {
case 90...100:
return "A"
case 80..<90:
return "B"
case 70..<80:
return "C"
default:
return "F"
}
}
struct Student {
var name: String
var score: Int
func report() {
print("\(name): \(score) (\(grade(for: score)))")
}
}
let student = Student(name: "Ana", score: 92)
student.report()Average and a missing name
The loop adds the three scores through the sorted keys, and the average is the total split by the count of students. The lookup for Eve finds no score, so the fallback line prints.
let scores = ["Ana": 92, "Bo": 74, "Cy": 85]
var total = 0
for name in scores.keys.sorted() {
total += scores[name] ?? 0
}
print(total / scores.count)
if let score = scores["Eve"] {
print(score)
} else {
print("No score for Eve")
}Example
let scores = ["Ana": 92, "Bo": 74, "Cy": 85, "Dee": 61]
func grade(for score: Int) -> String {
switch score {
case 90...100:
return "A"
case 80..<90:
return "B"
case 70..<80:
return "C"
default:
return "F"
}
}
struct Student {
var name: String
var score: Int
func report() {
print("\(name): \(score) (\(grade(for: score)))")
}
}
var total = 0
for name in scores.keys.sorted() {
let score = scores[name] ?? 0
let student = Student(name: name, score: score)
student.report()
total += score
}
print("Average: \(total / scores.count)")
if let score = scores["Eve"] {
print("Eve has \(score)")
} else {
print("No score for Eve")
}Key points
- A dictionary pairs each student name with one score.
- Walking the sorted keys prints the report in name order every run.
- A grade function maps a score to a letter with a switch over ranges.
- A Student struct holds a name and a score and prints one report line.
- A loop over the scores can add them up for a class average.
- A lookup for a missing name can print a fallback line instead of a score.
In this example, the scores dictionary pairs four names with their scores, and the grade function maps a score to a letter through a switch over ranges. The Student struct prints one report line per student with its report method. The loop walks the sorted keys, builds a student for each pair, prints the report line, and adds each score to the total. The average print divides that total by the count of students, and the final lookup for Eve finds no entry, so the fallback line prints.
Try it Yourself
Edit the code, then press Run.
Running is simulated ยท Programiz Swift Compiler
Exercise
With the grade function from this lesson, what letter does a score of 85 get?