Menu
CoddyTech

Elenco per corso

Fa parte della sezione Logica e flusso del percorso GDScript di Coddy. Lezione 47 di 78.

challenge icon

Sfida

Facile

Elenca gli studenti che frequentano un corso. Mantieni tutto ciò che hai scritto nei passaggi precedenti.

1. La funzione. Sotto le altre funzioni, scrivi list_students_by_course(course: String) -> Array. Scorre student_records e restituisce un array con i nomi degli studenti il cui set "courses" contiene il corso, nell'ordine in cui gli studenti sono stati aggiunti. Quando nessuno frequenta il corso, restituisce un array vuoto.

2. Il comando. Aggiungi un ramo per course <course>, ad esempio course Math, sopra ["show"]. Stampa l'array restituito da list_students_by_course(). Per i comandi

add Alice 20 Math Physics
add Bob 22 Math Biology
add Diana 23 Chemistry Physics
course Math
course Physics
course Biology
course History
exit

l'output è:

Student 'Alice' added successfully.
Student 'Bob' added successfully.
Student 'Diana' added successfully.
["Alice", "Bob"]
["Alice", "Diana"]
["Bob"]
[]
Goodbye!

Provalo tu

extends Node

# Ogni studente: nome -> {"age": int, "grades": Array, "courses": un insieme di nomi di corsi}
var student_records := {}

func _ready():
	while true:
		var parts := Array(OS.read_string_from_stdin().split(" ", false))
		match parts:
			["add", var student, var age, ..]:
				add_student(student, int(age), parts.slice(3))
			["grade", var student, var grade]:
				add_grade(student, int(grade))
			["enrolled", var student, var course]:
				print(is_enrolled(student, course))
			["average", var student]:
				var average := calculate_average_grade(student)
				if average >= 0:
					print("Average grade of '%s': %.2f" % [student, average])
			["show"]:
				print(student_records)
			["exit"]:
				print("Goodbye!")
				break

func add_student(student_name: String, age: int, courses: Array) -> void:
	if student_records.has(student_name):
		print("Student '%s' already exists." % student_name)
		return
	var course_set := {}
	for course in courses:
		course_set[course] = true
	student_records[student_name] = {"age": age, "grades": [], "courses": course_set}
	print("Student '%s' added successfully." % student_name)

func add_grade(student_name: String, grade: int) -> void:
	if not student_records.has(student_name):
		print("Student '%s' not found." % student_name)
		return
	student_records[student_name]["grades"].append(grade)
	print("Grade %d added for student '%s'." % [grade, student_name])

func is_enrolled(student_name: String, course: String) -> bool:
	if not student_records.has(student_name):
		print("Student '%s' not found." % student_name)
		return false
	return student_records[student_name]["courses"].has(course)

func calculate_average_grade(student_name: String) -> float:
	if not student_records.has(student_name):
		print("Student '%s' not found." % student_name)
		return -1.0
	var grades: Array = student_records[student_name]["grades"]
	if grades.is_empty():
		return 0.0
	var total := 0
	for grade in grades:
		total += grade
	return float(total) / grades.size()

Tutte le lezioni di Logica e flusso

Esercitati da solo: Compilatore GDScript online