Menu
CoddyTech

Aggiungi voto

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

challenge icon

Sfida

Facile

Registra i voti degli studenti. Conserva tutto ciò che hai scritto nei passaggi precedenti.

1. La funzione. Sotto le altre funzioni, scrivi add_grade(student_name: String, grade: int) -> void:

  • Se student_records non contiene il nome, stampa Student '<name>' not found. e restituisci.
  • Altrimenti aggiungi il voto all'array "grades" dello studente e stampa Grade <grade> added for student '<name>'.

I voti sono un array, non un insieme, perché uno studente può ricevere lo stesso voto due volte ed entrambi contano.

2. Il comando. Aggiungi un ramo per grade <name> <grade>, come grade Alice 90, sopra ["show"]. Chiama add_grade() convertendo il voto in un int. Per i comandi

add Alice 20 Math Physics
add Bob 22 Biology Chemistry
grade Alice 90
grade Alice 85
grade Bob 75
grade Charlie 80
show
exit

l'output è:

Student 'Alice' added successfully.
Student 'Bob' added successfully.
Grade 90 added for student 'Alice'.
Grade 85 added for student 'Alice'.
Grade 75 added for student 'Bob'.
Student 'Charlie' not found.
{ "Alice": { "age": 20, "grades": [90, 85], "courses": { "Math": true, "Physics": true } }, "Bob": { "age": 22, "grades": [75], "courses": { "Biology": true, "Chemistry": true } } }
Goodbye!

Provalo tu

extends Node

# Ogni studente: name -> {"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))
			["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)

Tutte le lezioni di Logica e flusso

Esercitati da solo: Compilatore GDScript online