As your keys in the dictionary are different, do the get
putting only one of the names, will not work.
If you repair well, in fact your code is looping in the return of the note key, thus printing the 10 string in two lines:
for nota in disciplinas.get('nota'):
print(nota)
Unfortunately it is far from what you wish.
There are numerous ways you can implement this, one of them is to print out all the keys that start with the word note, to know the keys of a dictionary, you can use the method keys
:
disciplinas.keys()
Having the keys in hand, you check if the key starts with the word note, for this, use the method startswith
:
chave.startswith("nota")
Now putting it all together, you would have the following code within the function AlunoeNota
:
for key in disciplinas.keys():
if key.startswith("nota"):
print(disciplinas[key])
This whole instruction can be in just one line:
[print(disciplinas[key]) for key in disciplinas.keys() if key.startswith("nota")]
Another way is to change the function AlunoeNota
to receive a list of keys that will be printed:
def AlunoeNota(disciplinas, nome, chaves):
print('Aluno da FHO ' + nome + ' - SI' )
for chave in chaves:
print(disciplinas[chave])
Then when you call the function, you send the third parameter telling which keys you want to print:
AlunoeNota(dicfinal,'Duener', ["nota","nota2"])
As a last suggestion, you can create a list of dictionaries, with this, you will only have a note key, which personally I think is much better.
I would have both dictionaries:
python = {'nome': 'Python', 'sigla': 'F0D4', 'periodo': 'Noturno', 'nota': '10'}
redes = {'nome': 'Redes', 'sigla': 'CH4T0', 'periodo': 'Noturno', 'nota': '8'}
Put them both on a list:
notasAluno = [python, redes]
Send the list to the function AlunoeNota
:
AlunoeNota(notasAluno,'Duener')
And the function, would loop in the list, always printing the note key:
def AlunoeNota(disciplinas, nome):
print('Aluno da FHO ' + nome + ' - SI' )
for dado in disciplinas:
print(dado["nota"])
With this, you can access the dictionary data in a much better way, and even format the output, for example:
print(f'A matéria {dado["nome"]} teve a nota {dado["nota"]}')
I had to change the keys because if not, the update that is the merge would not replace the current one. Is there any other way?
– Duener