Combine lists, loops, and conditionals to print a letter grade for each student in a class.
You start with:
names := []string{"Alice", "Bob", "Carol", "Dan"}
scores := []int{92, 78, 85, 67}
Build:
Alice: A Bob: C Carol: B Dan: D
Rules:
- Loop over the indices of the students (
len(names)) so you can read the name and the score at the same position. - Use the same scale as V3: 90+ A, 80-89 B, 70-79 C, 60-69 D, below 60 F.
- On each iteration print the name, a colon and a space, then the grade, for example
Alice: A.
Use
[]string and []int slices with index loops.
names := []string{"Alice", "Bob", "Carol", "Dan"}
scores := []int{92, 78, 85, 67}
for i := 0; i < len(names); i++ {
score := scores[i]
var grade string
if score >= 90 {
grade = "A"
} else if score >= 80 {
grade = "B"
} else if score >= 70 {
grade = "C"
} else if score >= 60 {
grade = "D"
} else {
grade = "F"
}
fmt.Println(names[i] + ": " + grade)
}
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
