Menu
CoddyTech

Add Student

Part of the Logic & Flow section of Coddy's GDScript journey. Lesson 43 of 78.

challenge icon

Challenge

Easy

Add students to the records. Keep everything you wrote in the previous steps.

1. The function. Below _ready(), write add_student(student_name: String, age: int, courses: Array) -> void:

  • If student_records already has the name, print Student '<name>' already exists. and return.
  • Otherwise build a set of the courses (a dictionary with each course as a key and true as its value) and store the student under their name as a dictionary with three keys: "age" (the age), "grades" (an empty array) and "courses" (the set). Then print Student '<name>' added successfully.

2. The command. The command add Alice 20 Math Physics holds the name, the age and any number of courses. Add this branch to the match, above ["show"]:

["add", var student, var age, ..]:
	add_student(student, int(age), parts.slice(3))

.. lets the pattern match the courses that follow, and parts.slice(3) is the array of those courses. For the commands

add Alice 20 Math Physics
add Bob 22 Biology Chemistry
show
exit

the output is:

Student 'Alice' added successfully.
Student 'Bob' added successfully.
{ "Alice": { "age": 20, "grades": [], "courses": { "Math": true, "Physics": true } }, "Bob": { "age": 22, "grades": [], "courses": { "Biology": true, "Chemistry": true } } }
Goodbye!

Try it yourself

extends Node

# Every student: name -> {"age": int, "grades": Array, "courses": a set of course names}
var student_records := {}

func _ready():
	while true:
		var parts := Array(OS.read_string_from_stdin().split(" ", false))
		match parts:
			["show"]:
				print(student_records)
			["exit"]:
				print("Goodbye!")
				break

All lessons in Logic & Flow

Practice on your own: Online GDScript compiler